• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • tools
  • nepomuksearch
tools/nepomuksearch/main.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013 Vishesh Handa <me@vhanda.in>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License as
6  published by the Free Software Foundation; either version 2 of
7  the License or (at your option) version 3 or any later version
8  accepted by the membership of KDE e.V. (or its successor approved
9  by the membership of KDE e.V.), which shall act as a proxy
10  defined in Section 14 of version 3 of the license.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <QtCore/QCoreApplication>
22 #include <QtCore/QUrl>
23 
24 #include <kcmdlineargs.h>
25 #include <kaboutdata.h>
26 #include <KDE/KLocale>
27 #include <KComponentData>
28 #include <KUrl>
29 #include <KDebug>
30 
31 #include <Soprano/Model>
32 #include <Soprano/QueryResultIterator>
33 #include <Soprano/Vocabulary/NAO>
34 
35 #include "nie.h"
36 #include "nmo.h"
37 #include "query.h"
38 #include "resultiterator.h"
39 #include "queryparser.h"
40 #include "resourcemanager.h"
41 #include "resource.h"
42 
43 using namespace Soprano::Vocabulary;
44 using namespace Nepomuk2::Vocabulary;
45 using namespace Nepomuk2;
46 
47 QString highlightBold(const QString& input) {
48  QLatin1String colorStart("\033[0;33m");
49  QLatin1String colorEnd("\033[0;0m");
50 
51  QString out(input);
52  out.replace( "<b>", colorStart );
53  out.replace( "</b>", colorEnd );
54 
55  return out;
56 }
57 
58 QString colorString(const QString& input, int color) {
59  QString colorStart = QString::fromLatin1("\033[0;%1m").arg( color );
60  QLatin1String colorEnd("\033[0;0m");
61 
62  return colorStart + input + colorEnd;
63 }
64 
65 QString fetchProperty(const QUrl& uri, const QUrl& prop) {
66  QString query = QString::fromLatin1("select ?o where { %1 %2 ?o . } LIMIT 1")
67  .arg( Soprano::Node::resourceToN3(uri),
68  Soprano::Node::resourceToN3(prop) );
69 
70  Soprano::Model* model = Nepomuk2::ResourceManager::instance()->mainModel();
71  Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
72  if( it.next() )
73  return it[0].toString();
74 
75  return QString();
76 }
77 
78 int main( int argc, char *argv[] )
79 {
80  KAboutData aboutData( "nepomuksearch",
81  "nepomuksearch",
82  ki18n("Nepomuk Search"),
83  "0.1",
84  ki18n("Nepomuk Search - A debugging tool"),
85  KAboutData::License_GPL,
86  ki18n("(c) 2013, Vishesh Handa"),
87  KLocalizedString(),
88  "http://nepomuk.kde.org" );
89  aboutData.addAuthor(ki18n("Vishesh Handa"),ki18n("Maintainer"), "me@vhanda.in");
90  aboutData.setProgramIconName( "nepomuk" );
91 
92  KCmdLineArgs::init( argc, argv, &aboutData );
93 
94  KCmdLineOptions options;
95  options.add("+query", ki18n("The words to search for"));
96  KCmdLineArgs::addCmdLineOptions( options );
97 
98  KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
99  if( args->count() == 0 ) {
100  KCmdLineArgs::usage();
101  return 1;
102  }
103 
104  QCoreApplication app( argc, argv );
105  KComponentData comp( aboutData );
106 
107  QTextStream out( stdout );
108 
109  ResourceManager* rm = ResourceManager::instance();
110  if( !rm->initialized() ) {
111  out << "Could not connect to Nepomuk";
112  return 1;
113  }
114 
115  // HACK: Find a better way!
116  QStringList argList = args->allArguments();
117  argList.takeFirst();
118  QString queryStr = argList.join(" ");
119 
120  Query::Query query = Query::QueryParser::parseQuery( queryStr );
121  query.addRequestProperty( Query::Query::RequestProperty( NIE::url(), false ) );
122  query.setQueryFlags( Query::Query::WithFullTextExcerpt );
123  query.setLimit( 10 );
124 
125  out << "\n";
126  Query::ResultIterator iter( query );
127  while( iter.next() ) {
128  Query::Result result = iter.current();
129  const QUrl url = result.requestProperty( NIE::url() ).uri();
130 
131  QString title;
132  if( url.isLocalFile() ) {
133  title = colorString(url.toLocalFile(), 32);
134  }
135  else {
136  QUrl resUri = result.resource().uri();
137  title = colorString(url.toString(), 32) + " " +
138  colorString(fetchProperty(resUri, NMO::sentDate()), 31) + " " +
139  colorString(fetchProperty(resUri, NAO::prefLabel()), 32);
140  }
141 
142  out << " " << title << endl;
143  out << " " << highlightBold( result.excerpt() ) << endl;
144  out << endl;
145  }
146 
147  return 0;
148 }
highlightBold
QString highlightBold(const QString &input)
Definition: tools/nepomuksearch/main.cpp:47
Nepomuk2::Query::ResultIterator::current
Result current() const
Definition: resultiterator.cpp:67
Nepomuk2::Query::ResultIterator::next
bool next()
Get the next result.
Definition: resultiterator.cpp:100
Nepomuk2::Query::ResultIterator
A class to iterate over Nepomuk query results.
Definition: resultiterator.h:45
Nepomuk2::Query::Query::setLimit
void setLimit(int)
Set the maximum number of results this query should yield.
Definition: query.cpp:317
query.h
Nepomuk2::Query::Query::addRequestProperty
void addRequestProperty(const RequestProperty &property)
Add a property that should be reported with each search result.
Definition: query.cpp:365
fetchProperty
QString fetchProperty(const QUrl &uri, const QUrl &prop)
Definition: tools/nepomuksearch/main.cpp:65
colorString
QString colorString(const QString &input, int color)
Definition: tools/nepomuksearch/main.cpp:58
Nepomuk2::ResourceManager::initialized
bool initialized() const
Definition: resourcemanager.cpp:306
Nepomuk2::Query::Query
A Nepomuk desktop query.
Definition: query.h:76
Nepomuk2::Query::Query::RequestProperty
A request property can be added to a Query to retrieve additional information about the results...
Definition: query.h:287
resource.h
Nepomuk2::ResourceManager::instance
static ResourceManager * instance()
Definition: resourcemanager.cpp:270
resourcemanager.h
resultiterator.h
Nepomuk2::Query::Result
A single search result.
Definition: result.h:57
Nepomuk2::ResourceManager
The ResourceManager is the central Nepomuk configuration point.
Definition: resourcemanager.h:55
Nepomuk2::Query::Result::excerpt
QString excerpt() const
An excerpt of the matched text with highlighted search words in case the query contained a full text ...
Definition: result.cpp:147
Nepomuk2::Query::Query::setQueryFlags
void setQueryFlags(QueryFlags flags)
Set the query flags to configure this query.
Definition: query.cpp:353
Nepomuk2::Query::Result::resource
Resource resource() const
The result resource.
Definition: result.cpp:81
Nepomuk2::Resource::uri
QUrl uri() const
The URI of the resource, uniquely identifying it.
Definition: resource.cpp:166
Nepomuk2::ResourceManager::mainModel
Soprano::Model * mainModel()
Retrieve the main data storage model.
Definition: resourcemanager.cpp:363
main
int main(int argc, char *argv[])
Definition: tools/nepomuksearch/main.cpp:78
Nepomuk2::Query::parseQuery
Query parseQuery(const QString &s)
Definition: queryserializer.cpp:411
queryparser.h
Nepomuk2::Query::Result::requestProperty
Soprano::Node requestProperty(const Types::Property &property) const
Retrieve value of request property property.
Definition: result.cpp:105
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal