1 | /* |
2 | * Copyright (C) 2002, 2003 David Faure <faure@kde.org> |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Library General Public |
6 | * License version 2 as published by the Free Software Foundation; |
7 | * |
8 | * This library is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Library General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU Library General Public License |
14 | * along with this library; see the file COPYING.LIB. If not, write to |
15 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
16 | * Boston, MA 02110-1301, USA. |
17 | */ |
18 | |
19 | #include <kcmdlineargs.h> |
20 | #include <kmimetypetrader.h> |
21 | #include <kmimetype.h> |
22 | #include <kapplication.h> |
23 | #include <klocale.h> |
24 | #include <kservicetypetrader.h> |
25 | |
26 | #include <stdio.h> |
27 | |
28 | int main( int argc, char **argv ) |
29 | { |
30 | KCmdLineArgs::init( argc, argv, "ktraderclient" , 0, ki18n("KTraderClient" ), "0.0" , ki18n("A command-line tool for querying the KDE trader system" )); |
31 | |
32 | |
33 | KCmdLineOptions options; |
34 | |
35 | options.add("mimetype <mimetype>" , ki18n("A mimetype" )); |
36 | |
37 | options.add("servicetype <servicetype>" , ki18n("A servicetype, like KParts/ReadOnlyPart or KMyApp/Plugin" )); |
38 | |
39 | options.add("constraint <constraint>" , ki18n("A constraint expressed in the trader query language" )); |
40 | |
41 | KCmdLineArgs::addCmdLineOptions( options ); |
42 | |
43 | KApplication app( false ); // no GUI |
44 | |
45 | KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); |
46 | |
47 | const QString mimetype = args->getOption( "mimetype" ); |
48 | QString servicetype = args->getOption( "servicetype" ); |
49 | const QString constraint = args->getOption( "constraint" ); |
50 | |
51 | if ( mimetype.isEmpty() && servicetype.isEmpty() ) |
52 | KCmdLineArgs::usage(); |
53 | |
54 | if ( !mimetype.isEmpty() ) |
55 | printf( "mimetype is : %s\n" , qPrintable( mimetype ) ); |
56 | if ( !servicetype.isEmpty() ) |
57 | printf( "servicetype is : %s\n" , qPrintable( servicetype ) ); |
58 | if ( !constraint.isEmpty() ) |
59 | printf( "constraint is : %s\n" , qPrintable( constraint ) ); |
60 | |
61 | KService::List offers; |
62 | if ( !mimetype.isEmpty() ) { |
63 | if ( servicetype.isEmpty() ) |
64 | servicetype = "Application" ; |
65 | offers = KMimeTypeTrader::self()->query( mimetype, servicetype, constraint ); |
66 | } |
67 | else |
68 | offers = KServiceTypeTrader::self()->query( servicetype, constraint ); |
69 | |
70 | printf("got %d offers.\n" , offers.count()); |
71 | |
72 | int i = 0; |
73 | KService::List::ConstIterator it = offers.constBegin(); |
74 | const KService::List::ConstIterator end = offers.constEnd(); |
75 | for (; it != end; ++it, ++i ) |
76 | { |
77 | printf("---- Offer %d ----\n" , i); |
78 | QStringList props = (*it)->propertyNames(); |
79 | QStringList::ConstIterator propIt = props.constBegin(); |
80 | QStringList::ConstIterator propEnd = props.constEnd(); |
81 | for (; propIt != propEnd; ++propIt ) |
82 | { |
83 | QVariant prop = (*it)->property( *propIt ); |
84 | |
85 | if ( !prop.isValid() ) |
86 | { |
87 | printf("Invalid property %s\n" , (*propIt).toLocal8Bit().data()); |
88 | continue; |
89 | } |
90 | |
91 | QString outp = *propIt; |
92 | outp += " : '" ; |
93 | |
94 | switch ( prop.type() ) |
95 | { |
96 | case QVariant::StringList: |
97 | outp += prop.toStringList().join(" - " ); |
98 | break; |
99 | case QVariant::Bool: |
100 | outp += prop.toBool() ? "TRUE" : "FALSE" ; |
101 | break; |
102 | default: |
103 | outp += prop.toString(); |
104 | break; |
105 | } |
106 | |
107 | if ( !outp.isEmpty() ) |
108 | printf("%s'\n" , outp.toLocal8Bit().data()); |
109 | } |
110 | } |
111 | return 0; |
112 | } |
113 | |
114 | |