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

console/kabcclient

  • sources
  • kde-4.14
  • kdepim
  • console
  • kabcclient
  • src
main.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2003 - 2004 Tobias Koenig <tokoe@kde.org>
3 // Copyright (C) 2005 - 2006 Kevin Krammer <kevin.krammer@gmx.at>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 //
19 
20 // local includes
21 #include "formatfactory.h"
22 #include "inputformat.h"
23 #include "kabcclient.h"
24 #include "outputformat.h"
25 
26 // standard includes
27 #include <iostream>
28 #include <sstream>
29 #include <stdlib.h>
30 
31 // Qt includes
32 #include <QtCore/QFileInfo>
33 
34 // KDE includes
35 #include <kaboutdata.h>
36 #include <kapplication.h>
37 #include <kcmdlineargs.h>
38 #include <klocalizedstring.h>
39 static const char version[] = "0.8.1";
40 
41 bool checkForFormatHelp(KCmdLineArgs* args, FormatFactory* factory);
42 bool checkForCodecHelp(KCmdLineArgs* args);
43 
44 int handleKABC2Mutt(int argc, char** argv);
45 
46 void avoidQPixmapWarning(QtMsgType type, const char *msg);
47 
48 QString optionAsString(KCmdLineArgs* args, const char* option);
49 
50 using std::cout;
51 using std::endl;
52 
54 
55 int main(int argc, char** argv)
56 {
57  QString commandName = QFileInfo(QFile::decodeName(argv[0])).fileName();
58  if (commandName == QLatin1String("kabc2mutt"))
59  {
60  return handleKABC2Mutt(argc, argv);
61  }
62 
63  KAboutData aboutData("kabcclient", "kabcclient", ki18n("KABC client"), version,
64  ki18n("KDE address book command-line client"),
65  KAboutData::License_GPL_V2);
66 
67  aboutData.addAuthor(ki18n("Kevin Krammer"), ki18n("Primary Author"), "kevin.krammer@gmx.at");
68 
69  KCmdLineOptions cmdLineOptions;
70 
71  cmdLineOptions.add("A");
72 
73  cmdLineOptions.add("add", ki18n("Add input data as new address book entries"));
74 
75  cmdLineOptions.add("R");
76 
77  cmdLineOptions.add("remove", ki18n("Remove entries matching the input data"));
78 
79  cmdLineOptions.add("M");
80 
81  cmdLineOptions.add("merge", ki18n("Merge input data into the address book"));
82 
83  cmdLineOptions.add("S");
84 
85  cmdLineOptions.add("search", ki18n("Search for entries matching the input data"));
86 
87  cmdLineOptions.add("L");
88 
89  cmdLineOptions.add("list", ki18n("List all entries in address book"));
90 
91  cmdLineOptions.add("nosave", ki18n("Do not save changes to the address book on add/remove operations"));
92 
93  cmdLineOptions.add("if");
94 
95  cmdLineOptions.add("input-format <format>", ki18n("How to interpret the input data."), "search");
96 
97  cmdLineOptions.add("if-opts");
98 
99  cmdLineOptions.add("input-format-options <options>", ki18n("Input options for the selected format"));
100 
101  cmdLineOptions.add("of");
102 
103  cmdLineOptions.add("output-format <format>", ki18n("How to present the output data."), "vcard");
104 
105  cmdLineOptions.add("of-opts");
106 
107  cmdLineOptions.add("output-format-options <options>", ki18n("Output options for the selected format"));
108 
109  cmdLineOptions.add("ic");
110 
111  cmdLineOptions.add("input-codec <textcodec>", ki18n("How to convert the input text."), "local");
112 
113  cmdLineOptions.add("oc");
114 
115  cmdLineOptions.add("output-codec <textcodec>", ki18n("How to convert the output text."), "local");
116 
117  cmdLineOptions.add("match-case", ki18n("Match key fields case sensitive. UID is always matched case sensitive"));
118 
119  cmdLineOptions.add(ki18n("+[input data]").toString().toLocal8Bit(), ki18n("Input to use instead of reading stdin"));
120 
121  KCmdLineArgs::addCmdLineOptions(cmdLineOptions);
122  KCmdLineArgs::init(argc, argv, &aboutData);
123 
124  KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
125 
126  qInstallMsgHandler(avoidQPixmapWarning);
127  KApplication app;
128  qInstallMsgHandler(0);
129 
130  FormatFactory formatFactory;
131 
132  if (checkForFormatHelp(args, &formatFactory)) return 0;
133 
134  if (checkForCodecHelp(args)) return 0;
135 
136  KABCClient::Operation operation = KABCClient::Search;
137  if (args->isSet("search"))
138  {
139  operation = KABCClient::Search;
140  }
141  else if (args->isSet("list"))
142  {
143  operation = KABCClient::List;
144  }
145  else if (args->isSet("add"))
146  {
147  operation = KABCClient::Add;
148  }
149  else if (args->isSet("merge"))
150  {
151  operation = KABCClient::Merge;
152  }
153  else if (args->isSet("remove"))
154  {
155  operation = KABCClient::Remove;
156  }
157  else
158  {
159  std::cerr << i18n("No operation specified, assuming --search").toLocal8Bit().data() << endl;
160  }
161 
162  KABCClient client(operation, &formatFactory);
163 
164  if (!client.setInputFormat(args->getOption("input-format").toLocal8Bit()))
165  {
166  const QString error = i18n("Invalid input format \"%1\". See --input-format help",
167  optionAsString(args, "input-format"));
168  KCmdLineArgs::usageError(error);
169  return 1;
170  }
171 
172  if (args->isSet("input-format-options"))
173  {
174  if (!client.setInputOptions(args->getOption("input-format-options").toLocal8Bit()))
175  {
176  const QString error = i18n("Invalid options for input format \"%1\". "
177  "See --input-format-options help",
178  optionAsString(args, "input-format"));
179  KCmdLineArgs::usageError(error);
180  }
181  }
182 
183  if (!client.setOutputFormat(args->getOption("output-format").toLocal8Bit()))
184  {
185  const QString error = i18n("Invalid output format \"%1\". See --output-format help",
186  optionAsString(args, "output-format"));
187  KCmdLineArgs::usageError(error);
188  return 1;
189  }
190 
191  if (args->isSet("output-format-options"))
192  {
193  if (!client.setOutputOptions(args->getOption("output-format-options").toLocal8Bit()))
194  {
195  const QString error = i18n("Invalid options for output format \"%1\". "
196  "See --output-format-options help",
197  optionAsString(args, "output-format"));
198  KCmdLineArgs::usageError(error);
199  }
200  }
201 
202  QString codecName = args->getOption("input-codec");
203  if (!args->isSet("input-codec") && args->getOption("input-format") == QLatin1String("vcard"))
204  codecName = QLatin1String("UTF8");
205 
206  if (!client.setInputCodec(codecName.toLocal8Bit()))
207  {
208  const QString error = i18n("Invalid input codec \"%1\"", codecName);
209  KCmdLineArgs::usageError(error);
210  return 1;
211  }
212 
213  codecName = args->getOption("output-codec");
214  if (!args->isSet("output-codec") && args->getOption("output-format") == QLatin1String("vcard"))
215  codecName = QLatin1String("UTF8");
216 
217  if (!client.setOutputCodec(codecName.toLocal8Bit()))
218  {
219  const QString error = i18n("Invalid output codec \"%1\"", codecName);
220  KCmdLineArgs::usageError(error);
221  return 1;
222  }
223 
224  if (args->isSet("match-case"))
225  {
226  client.setMatchCaseSensitivity(Qt::CaseSensitive);
227  }
228 
229  if (!args->isSet("save"))
230  {
231  client.setAllowSaving(false);
232  }
233 
234  std::stringstream sstream;
235  if (args->count() > 0)
236  {
237  for (int i = 0; i < args->count(); ++i)
238  {
239  sstream << args->arg(i).toLocal8Bit().data() << endl;
240  }
241 
242  client.setInputStream(&sstream);
243  }
244  else
245  {
246  client.setInputStream(&std::cin);
247  }
248 
249  if (!client.initOperation())
250  {
251  cout << i18n("Unable to perform requested operation").toLocal8Bit().data() << endl;
252  return 1;
253  }
254 
255  return app.exec();
256 }
257 
259 
260 bool checkForFormatHelp(KCmdLineArgs* args, FormatFactory* factory)
261 {
262  bool formatHelpRequested = false;
263 
264  if (args->isSet("input-format") && args->getOption("input-format") == QLatin1String("help"))
265  {
266  formatHelpRequested = true;
267 
268  cout << endl;
269  cout << i18n("The following input formats are available:").toLocal8Bit().data() << endl;
270 
271  QByteArrayList formats = factory->inputFormatList();
272  QByteArrayList::const_iterator it = formats.constBegin();
273  QByteArrayList::const_iterator endIt = formats.constEnd();
274  for (; it != endIt; ++it)
275  {
276  InputFormat* format = factory->inputFormat(*it);
277  if (format != 0)
278  {
279  cout << it->data() << ( (*it).length() >= 8 ? "\t" : "\t\t");
280 
281  QString description = format->description();
282  if (description.isEmpty()) description = i18n("No description available");
283 
284  cout << description.toLocal8Bit().data() << endl;
285  delete format;
286  }
287  }
288  }
289  else if (args->isSet("input-format-options") &&
290  args->getOption("input-format-options") == QLatin1String("help"))
291  {
292  formatHelpRequested = true;
293 
294  InputFormat* format = factory->inputFormat(args->getOption("input-format").toLocal8Bit());
295  if (format == 0)
296  {
297  const QString error = i18n("Invalid input format \"%1\". See --input-format help",
298  optionAsString(args, "input-format"));
299  KCmdLineArgs::usageError(error);
300  exit(1);
301  }
302 
303  QString usage = format->optionUsage();
304 
305  const QString tmparg = optionAsString(args, "input-format");
306  const QString message =
307  (usage.isEmpty()
308  ? i18n("No options available for input format %1", tmparg)
309  : i18n("The following options are available for input format %1:", tmparg));
310 
311  cout << endl;
312  cout << message.toLocal8Bit().data() << endl;
313  if (!usage.isEmpty()) cout << usage.toLocal8Bit().data() << endl;
314 
315  delete format;
316  }
317 
318  if (args->isSet("output-format") && args->getOption("output-format") == QLatin1String("help"))
319  {
320  formatHelpRequested = true;
321 
322  cout << endl;
323  cout << i18n("The following output formats are available:").toLocal8Bit().data() << endl;
324 
325  QByteArrayList formats = factory->outputFormatList();
326  QByteArrayList::const_iterator it = formats.constBegin();
327  QByteArrayList::const_iterator endIt = formats.constEnd();
328  for (; it != endIt; ++it)
329  {
330  OutputFormat* format = factory->outputFormat(*it);
331  if (format != 0)
332  {
333  cout << it->data() << ( (*it).length() >= 8 ? "\t" : "\t\t");
334 
335  QString description = format->description();
336  if (description.isEmpty()) description = i18n("No description available");
337 
338  cout << description.toLocal8Bit().data() << endl;
339  delete format;
340  }
341  }
342  }
343  else if (args->isSet("output-format-options") &&
344  args->getOption("output-format-options") == QLatin1String("help"))
345  {
346  formatHelpRequested = true;
347 
348  OutputFormat* format = factory->outputFormat(args->getOption("output-format").toLocal8Bit());
349  if (format == 0)
350  {
351  const QString error = i18n("Invalid output format \"%1\". See --output-format help",
352  optionAsString(args, "output-format"));
353  KCmdLineArgs::usageError(error);
354  exit(1);
355  }
356 
357  QString usage = format->optionUsage();
358 
359  const QString tmparg = optionAsString(args, "output-format");
360  const QString message =
361  (usage.isEmpty()
362  ? i18n("No options available for output format %1", tmparg)
363  : i18n("The following options are available for output format %1:", tmparg));
364 
365  cout << endl;
366  cout << message.toLocal8Bit().data() << endl;
367  if (!usage.isEmpty()) cout << usage.toLocal8Bit().data() << endl;
368 
369  delete format;
370  }
371 
372  return formatHelpRequested;
373 }
374 
376 
377 bool checkForCodecHelp(KCmdLineArgs* args)
378 {
379  bool codecHelpRequested = false;
380 
381  if (args->isSet("input-codec") && args->getOption("input-codec") == QLatin1String("help"))
382  {
383  codecHelpRequested = true;
384 
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;
389  }
390 
391  if (args->isSet("output-codec") && args->getOption("output-codec") == QLatin1String("help"))
392  {
393  codecHelpRequested = true;
394 
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;
399  }
400 
401  if (codecHelpRequested)
402  {
403  cout << i18n("Built-in codecs are UTF8 and LOCAL, respectively using "
404  "the 8-bit unicode format or your local encoding").toLocal8Bit().data()
405  << endl;
406 
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;
410  }
411 
412  return codecHelpRequested;
413 }
414 
417 
418 int handleKABC2Mutt(int argc, char** argv)
419 {
420  KAboutData aboutData("kabc2mutt", "kabcclient", ki18n("kabc2mutt"), version,
421  ki18n("kabc - mutt converter"),
422  KAboutData::License_GPL_V2);
423 
424  aboutData.addAuthor(ki18n("Tobias König"), ki18n("Primary Author"),
425  "tokoe@kde.org");
426  aboutData.addAuthor(ki18n("Kevin Krammer"), ki18n("Contributor"), "kevin.krammer@gmx.at");
427 
428 
429  KCmdLineOptions kabc2muttCmdLineOptions;
430 
431  kabc2muttCmdLineOptions.add("query <substring>", ki18n("Only show contacts where name or address matches <placeholder>substring</placeholder>"));
432 
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");
435 
436  kabc2muttCmdLineOptions.add("alternate-key-format", ki18n("Default key format is 'JohDoe', this option turns it into 'jdoe'"));
437 
438  kabc2muttCmdLineOptions.add("ignore-case", ki18n("Make queries case insensitive"));
439 
440  kabc2muttCmdLineOptions.add("all-addresses", ki18n("Return all mail addresses, not just the preferred one"));
441 
442  KCmdLineArgs::addCmdLineOptions(kabc2muttCmdLineOptions);
443  KCmdLineArgs::init(argc, argv, &aboutData);
444 
445  KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
446 
447  KABCClient::Operation operation = KABCClient::List;
448  if (args->isSet("query"))
449  {
450  operation = KABCClient::Search;
451  }
452 
453  FormatFactory formatFactory;
454  KABCClient client(operation, &formatFactory);
455 
456  client.setInputFormat("email");
457  client.setOutputFormat("mutt");
458  client.setInputCodec("UTF-8");
459  QString options = args->getOption("format");
460 
461  if (args->isSet("alternate-key-format"))
462  {
463  options.append(QLatin1String(",altkeys"));
464  }
465  if (args->isSet("all-addresses"))
466  {
467  options.append(QLatin1String(",allemails"));
468  }
469 
470  if (!args->isSet("ignore-case"))
471  {
472  client.setMatchCaseSensitivity(Qt::CaseSensitive);
473  }
474 
475  client.setOutputOptions(options.toLocal8Bit().data());
476 
477  std::stringstream sstream;
478  if (operation == KABCClient::Search)
479  {
480  sstream << args->getOption("query").toLocal8Bit().data() << endl;
481  client.setInputStream(&sstream);
482  }
483 
484  qInstallMsgHandler(avoidQPixmapWarning);
485  //KApplication::disableAutoDcopRegistration();
486  KApplication app(false);
487  qInstallMsgHandler(0);
488 
489  if (!client.initOperation())
490  {
491  cout << i18n("Unable to perform requested operation").toLocal8Bit().data() << endl;
492  return 1;
493  }
494 
495  // mutt wants a line of text before the results
496  cout << i18n("Searching KDE address book").toLocal8Bit().data() << endl;
497 
498  int result = app.exec();
499 
500  // in case of no match mutt wants a line of text saying so
501  if (result == 2) // Operation Search returns 2 on no match
502  cout << i18n("No matches in KDE address book").toLocal8Bit().data() << endl;
503 
504  return result;
505 }
506 
508 
509 void avoidQPixmapWarning(QtMsgType type, const char *msg)
510 {
511  Q_UNUSED(type)
512  Q_UNUSED(msg)
513 }
514 
516 
517 QString optionAsString(KCmdLineArgs* args, const char* option)
518 {
519  if (args == 0 || option == 0) return QString();
520 
521  return args->getOption(option);
522 }
523 
524 // End of file
avoidQPixmapWarning
void avoidQPixmapWarning(QtMsgType type, const char *msg)
Definition: main.cpp:509
KABCClient
Main handler of the program.
Definition: kabcclient.h:50
OutputFormat::description
virtual QString description() const =0
Returns a translate description of the output format.
KABCClient::setOutputFormat
bool setOutputFormat(const QByteArray &name)
Sets the output format to use.
Definition: kabcclient.cpp:110
QString::append
QString & append(QChar ch)
KABCClient::setInputFormat
bool setInputFormat(const QByteArray &name)
Sets the input format to use.
Definition: kabcclient.cpp:79
KABCClient::Add
Adds the input to the address book.
Definition: kabcclient.h:78
InputFormat::optionUsage
virtual QString optionUsage() const
Returns a translate message about the available format options.
Definition: inputformat.h:135
main
int main(int argc, char **argv)
Definition: main.cpp:55
KABCClient::Merge
Merges input data into the address book.
Definition: kabcclient.h:112
FormatFactory::inputFormatList
QByteArrayList inputFormatList() const
Returns a list of input parser names.
Definition: formatfactory.h:128
InputFormat
Interface for input format parsers.
Definition: inputformat.h:51
KABCClient::setOutputCodec
bool setOutputCodec(const QByteArray &name)
Sets the text codec for writing the output data.
Definition: kabcclient.cpp:154
KABCClient::setInputOptions
bool setInputOptions(const QByteArray &options)
Sets the options for the input format.
Definition: kabcclient.cpp:119
checkForCodecHelp
bool checkForCodecHelp(KCmdLineArgs *args)
Definition: main.cpp:377
KABCClient::setOutputOptions
bool setOutputOptions(const QByteArray &options)
Sets the options for the output format.
Definition: kabcclient.cpp:130
FormatFactory::outputFormatList
QByteArrayList outputFormatList() const
Returns a list of output formatter names.
Definition: formatfactory.h:143
OutputFormat
Interface for output formatters.
Definition: outputformat.h:53
KABCClient::initOperation
bool initOperation()
Checks if Operation setup is correct and schedules execution.
Definition: kabcclient.cpp:174
KABCClient::setMatchCaseSensitivity
void setMatchCaseSensitivity(Qt::CaseSensitivity sensitivity)
Sets the string matching mode.
Definition: kabcclient.h:260
FormatFactory::inputFormat
InputFormat * inputFormat(const QByteArray &name)
Creates an InputFormat instance for the given name.
Definition: formatfactory.cpp:53
formatfactory.h
KABCClient::setInputCodec
bool setInputCodec(const QByteArray &name)
Sets the text codec for reading the input data.
Definition: kabcclient.cpp:139
QFileInfo::fileName
QString fileName() const
FormatFactory::outputFormat
OutputFormat * outputFormat(const QByteArray &name)
Creates an OutputFormat instance for the given name.
Definition: formatfactory.cpp:81
QString::isEmpty
bool isEmpty() const
outputformat.h
handleKABC2Mutt
int handleKABC2Mutt(int argc, char **argv)
Definition: main.cpp:418
QString
QList< QByteArray >
QFileInfo
QString::toLocal8Bit
QByteArray toLocal8Bit() const
KABCClient::setInputStream
void setInputStream(std::istream *stream)
Sets the input stream to read data from.
Definition: kabcclient.cpp:167
optionAsString
QString optionAsString(KCmdLineArgs *args, const char *option)
Definition: main.cpp:517
OutputFormat::optionUsage
virtual QString optionUsage() const
Returns a translate message about the available format options.
Definition: outputformat.h:114
kabcclient.h
QLatin1String
KABCClient::Remove
Removes matching contact from the address book.
Definition: kabcclient.h:95
FormatFactory
Factory for input parsers and output formatters.
Definition: formatfactory.h:102
QByteArray::data
char * data()
KABCClient::Search
Searches for matching entries in the address book.
Definition: kabcclient.h:124
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
KABCClient::setAllowSaving
void setAllowSaving(bool on)
Sets the save behavior.
Definition: kabcclient.h:277
KABCClient::List
Writes all contacts of the address book.
Definition: kabcclient.h:65
checkForFormatHelp
bool checkForFormatHelp(KCmdLineArgs *args, FormatFactory *factory)
Definition: main.cpp:260
QFile::decodeName
QString decodeName(const QByteArray &localFileName)
KABCClient::Operation
Operation
List of supported operations.
Definition: kabcclient.h:58
inputformat.h
version
static const char version[]
Definition: main.cpp:39
InputFormat::description
virtual QString description() const =0
Returns a translate description of the input format.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:31:23 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

console/kabcclient

Skip menu "console/kabcclient"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

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