• 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
  • examples
converter.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005 - 2006 Kevin Krammer <kevin.krammer@gmx.at>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 //
18 
19 // standard includes
20 #include <iostream>
21 
22 // Qt includes
23 #include <QTextCodec>
24 
25 // KDE includes
26 #include <kcomponentdata.h>
27 
28 // KABC includes
29 #include <kabc/addressee.h>
30 
31 // local includes
32 #include "formatfactory.h"
33 #include "inputformat.h"
34 #include "outputformat.h"
35 
36 using std::cerr;
37 using std::cout;
38 using std::cin;
39 using std::endl;
40 
41 int main(int argc, char** argv)
42 {
43  // check if we have enough commandline parameters
44  if (argc != 3)
45  {
46  cerr << "Wrong number of arguments." << endl;
47  cerr << "Usage: converter inputformat outputformat" << endl;
48  return -1;
49  }
50 
51  // create format factory and check if the commandline parameters
52  // match format names
53  FormatFactory factory;
54 
55  InputFormat* input = factory.inputFormat(argv[1]);
56  if (input == 0)
57  {
58  cerr << "Program argument '" << argv[1] << endl;
59  cerr << "' does not match any of the known input format names" << endl;
60  return -1;
61  }
62 
63  OutputFormat* output = factory.outputFormat(argv[2]);
64  if (output == 0)
65  {
66  cerr << "Program argument '" << argv[2] << endl;
67  cerr << "' does not match any of the known output format names" << endl;
68 
69  // don't forget to delete the InputFormat instance we already have
70  delete input;
71 
72  return -1;
73  }
74 
75  // create codecs. just use local for this simple example
76  QTextCodec* codec = QTextCodec::codecForLocale();
77  if (!input->setCodec(codec))
78  {
79  cerr << "TextCodec '" << codec->name().data() << endl;
80  cerr << "' rejected by input format '" << argv[1] << endl;
81 
82  // don't forget to delete format instances
83  delete input;
84  delete output;
85 
86  return -1;
87  }
88 
89  codec = QTextCodec::codecForLocale();
90  if (!output->setCodec(codec))
91  {
92  cerr << "TextCodec '" << codec->name().data() << endl;
93  cerr << "' rejected by output format '" << argv[2] << endl;
94 
95  // don't forget to delete format instances
96  delete input;
97  delete output;
98 
99  return -1;
100  }
101 
102  // the KComponentData instance is needed for the translation calls
103  // inside the format implementations.
104  // in real programs this is usually the KApplication object
105  KComponentData instance("converter");
106 
107  // as long as there is possibly data on standard input
108  // try to read it and convert it
109  while (!cin.eof() && !cin.bad())
110  {
111  KABC::Addressee addressee = input->readAddressee(cin);
112 
113  // check if object actually contains anything
114  if (addressee.isEmpty())
115  {
116  cerr << "Failed to read an addressee." << endl;
117  }
118  else
119  {
120  output->writeAddressee(addressee, cout);
121  }
122  }
123 
124  delete input;
125  delete output;
126 
127  return 0;
128 }
129 
130 // End of File
QTextCodec::name
virtual QByteArray name() const =0
InputFormat
Interface for input format parsers.
Definition: inputformat.h:51
InputFormat::readAddressee
virtual KABC::Addressee readAddressee(std::istream &stream)=0
Reads one addressee from the input stream.
OutputFormat
Interface for output formatters.
Definition: outputformat.h:53
QTextCodec::codecForLocale
QTextCodec * codecForLocale()
FormatFactory::inputFormat
InputFormat * inputFormat(const QByteArray &name)
Creates an InputFormat instance for the given name.
Definition: formatfactory.cpp:53
OutputFormat::writeAddressee
virtual bool writeAddressee(const KABC::Addressee &addressee, std::ostream &stream)=0
Writes the data of a given addressee to the given output stream.
formatfactory.h
FormatFactory::outputFormat
OutputFormat * outputFormat(const QByteArray &name)
Creates an OutputFormat instance for the given name.
Definition: formatfactory.cpp:81
outputformat.h
OutputFormat::setCodec
virtual bool setCodec(QTextCodec *codec)=0
Sets the text codec to use.
QTextCodec
main
int main(int argc, char **argv)
Definition: converter.cpp:41
FormatFactory
Factory for input parsers and output formatters.
Definition: formatfactory.h:102
QByteArray::data
char * data()
InputFormat::setCodec
virtual bool setCodec(QTextCodec *codec)=0
Sets the text codec to use.
inputformat.h
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