32 #include <QtCore/QFileInfo>
35 #include <kaboutdata.h>
36 #include <kapplication.h>
37 #include <kcmdlineargs.h>
38 #include <klocalizedstring.h>
55 int main(
int argc,
char** argv)
57 QString commandName = QFileInfo(QFile::decodeName(argv[0])).fileName();
58 if (commandName == QLatin1String(
"kabc2mutt"))
63 KAboutData aboutData(
"kabcclient",
"kabcclient", ki18n(
"KABC client"),
version,
64 ki18n(
"KDE address book command-line client"),
65 KAboutData::License_GPL_V2);
67 aboutData.addAuthor(ki18n(
"Kevin Krammer"), ki18n(
"Primary Author"),
"kevin.krammer@gmx.at");
69 KCmdLineOptions cmdLineOptions;
71 cmdLineOptions.add(
"A");
73 cmdLineOptions.add(
"add", ki18n(
"Add input data as new address book entries"));
75 cmdLineOptions.add(
"R");
77 cmdLineOptions.add(
"remove", ki18n(
"Remove entries matching the input data"));
79 cmdLineOptions.add(
"M");
81 cmdLineOptions.add(
"merge", ki18n(
"Merge input data into the address book"));
83 cmdLineOptions.add(
"S");
85 cmdLineOptions.add(
"search", ki18n(
"Search for entries matching the input data"));
87 cmdLineOptions.add(
"L");
89 cmdLineOptions.add(
"list", ki18n(
"List all entries in address book"));
91 cmdLineOptions.add(
"nosave", ki18n(
"Do not save changes to the address book on add/remove operations"));
93 cmdLineOptions.add(
"if");
95 cmdLineOptions.add(
"input-format <format>", ki18n(
"How to interpret the input data."),
"search");
97 cmdLineOptions.add(
"if-opts");
99 cmdLineOptions.add(
"input-format-options <options>", ki18n(
"Input options for the selected format"));
101 cmdLineOptions.add(
"of");
103 cmdLineOptions.add(
"output-format <format>", ki18n(
"How to present the output data."),
"vcard");
105 cmdLineOptions.add(
"of-opts");
107 cmdLineOptions.add(
"output-format-options <options>", ki18n(
"Output options for the selected format"));
109 cmdLineOptions.add(
"ic");
111 cmdLineOptions.add(
"input-codec <textcodec>", ki18n(
"How to convert the input text."),
"local");
113 cmdLineOptions.add(
"oc");
115 cmdLineOptions.add(
"output-codec <textcodec>", ki18n(
"How to convert the output text."),
"local");
117 cmdLineOptions.add(
"match-case", ki18n(
"Match key fields case sensitive. UID is always matched case sensitive"));
119 cmdLineOptions.add(ki18n(
"+[input data]").toString().toLocal8Bit(), ki18n(
"Input to use instead of reading stdin"));
121 KCmdLineArgs::addCmdLineOptions(cmdLineOptions);
122 KCmdLineArgs::init(argc, argv, &aboutData);
124 KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
128 qInstallMsgHandler(0);
137 if (args->isSet(
"search"))
141 else if (args->isSet(
"list"))
145 else if (args->isSet(
"add"))
149 else if (args->isSet(
"merge"))
153 else if (args->isSet(
"remove"))
159 std::cerr << i18n(
"No operation specified, assuming --search").toLocal8Bit().data() << endl;
164 if (!client.
setInputFormat(args->getOption(
"input-format").toLocal8Bit()))
166 const QString error = i18n(
"Invalid input format \"%1\". See --input-format help",
168 KCmdLineArgs::usageError(error);
172 if (args->isSet(
"input-format-options"))
174 if (!client.
setInputOptions(args->getOption(
"input-format-options").toLocal8Bit()))
176 const QString error = i18n(
"Invalid options for input format \"%1\". "
177 "See --input-format-options help",
179 KCmdLineArgs::usageError(error);
183 if (!client.
setOutputFormat(args->getOption(
"output-format").toLocal8Bit()))
185 const QString error = i18n(
"Invalid output format \"%1\". See --output-format help",
187 KCmdLineArgs::usageError(error);
191 if (args->isSet(
"output-format-options"))
193 if (!client.
setOutputOptions(args->getOption(
"output-format-options").toLocal8Bit()))
195 const QString error = i18n(
"Invalid options for output format \"%1\". "
196 "See --output-format-options help",
198 KCmdLineArgs::usageError(error);
202 QString codecName = args->getOption(
"input-codec");
203 if (!args->isSet(
"input-codec") && args->getOption(
"input-format") == QLatin1String(
"vcard"))
204 codecName = QLatin1String(
"UTF8");
208 const QString error = i18n(
"Invalid input codec \"%1\"", codecName);
209 KCmdLineArgs::usageError(error);
213 codecName = args->getOption(
"output-codec");
214 if (!args->isSet(
"output-codec") && args->getOption(
"output-format") == QLatin1String(
"vcard"))
215 codecName = QLatin1String(
"UTF8");
219 const QString error = i18n(
"Invalid output codec \"%1\"", codecName);
220 KCmdLineArgs::usageError(error);
224 if (args->isSet(
"match-case"))
229 if (!args->isSet(
"save"))
234 std::stringstream sstream;
235 if (args->count() > 0)
237 for (
int i = 0; i < args->count(); ++i)
239 sstream << args->arg(i).toLocal8Bit().data() << endl;
251 cout << i18n(
"Unable to perform requested operation").toLocal8Bit().data() << endl;
262 bool formatHelpRequested =
false;
264 if (args->isSet(
"input-format") && args->getOption(
"input-format") == QLatin1String(
"help"))
266 formatHelpRequested =
true;
269 cout << i18n(
"The following input formats are available:").toLocal8Bit().data() << endl;
272 QByteArrayList::const_iterator it = formats.constBegin();
273 QByteArrayList::const_iterator endIt = formats.constEnd();
274 for (; it != endIt; ++it)
279 cout << it->data() << ( (*it).length() >= 8 ?
"\t" :
"\t\t");
282 if (description.isEmpty()) description = i18n(
"No description available");
284 cout << description.toLocal8Bit().data() << endl;
289 else if (args->isSet(
"input-format-options") &&
290 args->getOption(
"input-format-options") == QLatin1String(
"help"))
292 formatHelpRequested =
true;
297 const QString error = i18n(
"Invalid input format \"%1\". See --input-format help",
299 KCmdLineArgs::usageError(error);
306 const QString message =
308 ? i18n(
"No options available for input format %1", tmparg)
309 : i18n(
"The following options are available for input format %1:", tmparg));
312 cout << message.toLocal8Bit().data() << endl;
313 if (!usage.isEmpty()) cout << usage.toLocal8Bit().data() << endl;
318 if (args->isSet(
"output-format") && args->getOption(
"output-format") == QLatin1String(
"help"))
320 formatHelpRequested =
true;
323 cout << i18n(
"The following output formats are available:").toLocal8Bit().data() << endl;
326 QByteArrayList::const_iterator it = formats.constBegin();
327 QByteArrayList::const_iterator endIt = formats.constEnd();
328 for (; it != endIt; ++it)
333 cout << it->data() << ( (*it).length() >= 8 ?
"\t" :
"\t\t");
336 if (description.isEmpty()) description = i18n(
"No description available");
338 cout << description.toLocal8Bit().data() << endl;
343 else if (args->isSet(
"output-format-options") &&
344 args->getOption(
"output-format-options") == QLatin1String(
"help"))
346 formatHelpRequested =
true;
351 const QString error = i18n(
"Invalid output format \"%1\". See --output-format help",
353 KCmdLineArgs::usageError(error);
360 const QString message =
362 ? i18n(
"No options available for output format %1", tmparg)
363 : i18n(
"The following options are available for output format %1:", tmparg));
366 cout << message.toLocal8Bit().data() << endl;
367 if (!usage.isEmpty()) cout << usage.toLocal8Bit().data() << endl;
372 return formatHelpRequested;
379 bool codecHelpRequested =
false;
381 if (args->isSet(
"input-codec") && args->getOption(
"input-codec") == QLatin1String(
"help"))
383 codecHelpRequested =
true;
385 cout << i18n(
"The input codec transforms the input text data into an "
386 "universal internal format").toLocal8Bit().data() << endl;
387 cout << i18n(
"Default input encoding is 'local' unless input format is 'vcard', "
388 "in which case the default encoding will be 'utf8'.").toLocal8Bit().data() << endl;
391 if (args->isSet(
"output-codec") && args->getOption(
"output-codec") == QLatin1String(
"help"))
393 codecHelpRequested =
true;
395 cout << i18n(
"The output codec transforms the output text data from the "
396 "internal format to an 8-bit text format").toLocal8Bit().data() << endl;
397 cout << i18n(
"Default output encoding is 'local' unless output format is 'vcard', "
398 "in which case the default encoding will be 'utf8'.").toLocal8Bit().data() << endl;
401 if (codecHelpRequested)
403 cout << i18n(
"Built-in codecs are UTF8 and LOCAL, respectively using "
404 "the 8-bit unicode format or your local encoding").toLocal8Bit().data()
407 cout << i18n(
"Other codecs can be specified by their ISO code, for "
408 "example 'ISO 8859-15' for western european languages, "
409 "including the Euro sign").toLocal8Bit().data() << endl;
412 return codecHelpRequested;
420 KAboutData aboutData(
"kabc2mutt",
"kabcclient", ki18n(
"kabc2mutt"),
version,
421 ki18n(
"kabc - mutt converter"),
422 KAboutData::License_GPL_V2);
424 aboutData.addAuthor(ki18n(
"Tobias König"), ki18n(
"Primary Author"),
426 aboutData.addAuthor(ki18n(
"Kevin Krammer"), ki18n(
"Contributor"),
"kevin.krammer@gmx.at");
429 KCmdLineOptions kabc2muttCmdLineOptions;
431 kabc2muttCmdLineOptions.add(
"query <substring>", ki18n(
"Only show contacts where name or address matches <placeholder>substring</placeholder>"));
433 kabc2muttCmdLineOptions.add(
"format <format>", ki18n(
"Default format is 'alias'. 'query' returns email[tab]name[tab], "
434 "as needed by mutt's query_command"),
"alias");
436 kabc2muttCmdLineOptions.add(
"alternate-key-format", ki18n(
"Default key format is 'JohDoe', this option turns it into 'jdoe'"));
438 kabc2muttCmdLineOptions.add(
"ignore-case", ki18n(
"Make queries case insensitive"));
440 kabc2muttCmdLineOptions.add(
"all-addresses", ki18n(
"Return all mail addresses, not just the preferred one"));
442 KCmdLineArgs::addCmdLineOptions(kabc2muttCmdLineOptions);
443 KCmdLineArgs::init(argc, argv, &aboutData);
445 KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
448 if (args->isSet(
"query"))
459 QString options = args->getOption(
"format");
461 if (args->isSet(
"alternate-key-format"))
463 options.append(QLatin1String(
",altkeys"));
465 if (args->isSet(
"all-addresses"))
467 options.append(QLatin1String(
",allemails"));
470 if (!args->isSet(
"ignore-case"))
477 std::stringstream sstream;
480 sstream << args->getOption(
"query").toLocal8Bit().data() << endl;
486 KApplication app(
false);
487 qInstallMsgHandler(0);
491 cout << i18n(
"Unable to perform requested operation").toLocal8Bit().data() << endl;
496 cout << i18n(
"Searching KDE address book").toLocal8Bit().data() << endl;
498 int result = app.exec();
502 cout << i18n(
"No matches in KDE address book").toLocal8Bit().data() << endl;
519 if (args == 0 || option == 0)
return QString();
521 return args->getOption(option);
void avoidQPixmapWarning(QtMsgType type, const char *msg)
Main handler of the program.
bool setOutputFormat(const QByteArray &name)
Sets the output format to use.
bool setInputFormat(const QByteArray &name)
Sets the input format to use.
Adds the input to the address book.
int main(int argc, char **argv)
Merges input data into the address book.
bool setOutputCodec(const QByteArray &name)
Sets the text codec for writing the output data.
bool setInputOptions(const QByteArray &options)
Sets the options for the input format.
bool checkForCodecHelp(KCmdLineArgs *args)
bool setOutputOptions(const QByteArray &options)
Sets the options for the output format.
bool initOperation()
Checks if Operation setup is correct and schedules execution.
void setMatchCaseSensitivity(Qt::CaseSensitivity sensitivity)
Sets the string matching mode.
bool setInputCodec(const QByteArray &name)
Sets the text codec for reading the input data.
int handleKABC2Mutt(int argc, char **argv)
void setInputStream(std::istream *stream)
Sets the input stream to read data from.
QString optionAsString(KCmdLineArgs *args, const char *option)
Removes matching contact from the address book.
Searches for matching entries in the address book.
void setAllowSaving(bool on)
Sets the save behavior.
Writes all contacts of the address book.
bool checkForFormatHelp(KCmdLineArgs *args, FormatFactory *factory)
Operation
List of supported operations.
static const char version[]