~ubuntu-branches/ubuntu/oneiric/kig/oneiric

« back to all changes in this revision

Viewing changes to kig/main.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2011-07-10 11:57:38 UTC
  • Revision ID: james.westby@ubuntu.com-20110710115738-gdjnn1kctr49lmy9
Tags: upstream-4.6.90+repack
ImportĀ upstreamĀ versionĀ 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 This file is part of Kig, a KDE program for Interactive Geometry...
 
3
 Copyright (C) 2002  Dominique Devriese <devriese@kde.org>
 
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
 
18
 USA
 
19
**/
 
20
 
 
21
#include "kig.h"
 
22
 
 
23
#include <qbytearray.h>
 
24
 
 
25
#include <kuniqueapplication.h>
 
26
#include <kaboutdata.h>
 
27
#include <kcmdlineargs.h>
 
28
#include <klocale.h>
 
29
#include <klibloader.h>
 
30
#include <kdebug.h>
 
31
 
 
32
#include "aboutdata.h"
 
33
 
 
34
class KigApplication
 
35
  : public KUniqueApplication
 
36
{
 
37
public:
 
38
  KigApplication( bool gui = true );
 
39
  int newInstance();
 
40
  void handleArgs( KCmdLineArgs* args );
 
41
};
 
42
 
 
43
KigApplication::KigApplication( bool gui )
 
44
  : KUniqueApplication( gui, gui )
 
45
{
 
46
}
 
47
 
 
48
int KigApplication::newInstance()
 
49
{
 
50
  static bool first = true;
 
51
  if (isSessionRestored() && first)
 
52
  {
 
53
    first = false;
 
54
    return 0;
 
55
  }
 
56
  first = false;
 
57
 
 
58
  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
 
59
  handleArgs(args);
 
60
  args->clear();
 
61
  return 0;
 
62
}
 
63
 
 
64
void KigApplication::handleArgs( KCmdLineArgs* args )
 
65
{
 
66
  if ( args->count() == 0 )
 
67
  {
 
68
    Kig *widget = new Kig;
 
69
    widget->show();
 
70
  }
 
71
  else
 
72
  {
 
73
    for (int i = 0; i < args->count(); i++ )
 
74
    {
 
75
      Kig *widget = new Kig;
 
76
      widget->show();
 
77
      widget->load( args->url( i ) );
 
78
    }
 
79
  }
 
80
}
 
81
 
 
82
static int convertToNative( const KUrl& file, const QByteArray& outfile )
 
83
{
 
84
  KComponentData maindata( KCmdLineArgs::aboutData() );
 
85
  KLibrary* library = KLibLoader::self()->library( "kigpart", QLibrary::ExportExternalSymbolsHint );
 
86
  int ( *converterfunction )( const KUrl&, const QByteArray& );
 
87
  converterfunction = ( int ( * )( const KUrl&, const QByteArray& ) ) library->resolveFunction( "convertToNative" );
 
88
  if ( !converterfunction )
 
89
  {
 
90
    kError() << "Error: broken Kig installation: different library and application version !" << endl;
 
91
    return -1;
 
92
  }
 
93
  return (*converterfunction)( file, outfile );
 
94
}
 
95
 
 
96
int main(int argc, char **argv)
 
97
{
 
98
  KAboutData about = kigAboutData( "kig", I18N_NOOP("Kig") );
 
99
 
 
100
  KCmdLineArgs::init(argc, argv, &about);
 
101
 
 
102
  KCmdLineOptions options;
 
103
  options.add("c");
 
104
  options.add("convert-to-native", ki18n( "Do not show a GUI. Convert the specified file to the native Kig format. Output goes to stdout unless --outfile is specified." ));
 
105
  options.add("o");
 
106
  options.add("outfile <file>", ki18n( "File to output the created native file to. '-' means output to stdout. Default is stdout as well." ));
 
107
  options.add("+[URL]", ki18n( "Document to open" ));
 
108
  KCmdLineArgs::addCmdLineOptions( options );
 
109
  KigApplication::addCmdLineOptions();
 
110
 
 
111
  KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
 
112
  if ( args->isSet( "convert-to-native" ) )
 
113
  {
 
114
    QString outfile = args->getOption( "outfile" );
 
115
    if ( outfile.isEmpty() )
 
116
      outfile = "-";
 
117
 
 
118
    if ( args->count() == 0 )
 
119
    {
 
120
      kError() << "Error: --convert-to-native specified without a file to convert." << endl;
 
121
      return -1;
 
122
    }
 
123
    if ( args->count() > 1 )
 
124
    {
 
125
      kError() << "Error: --convert-to-native specified with more than one file to convert." << endl;
 
126
      return -1;
 
127
    }
 
128
    return convertToNative( args->url( 0 ), outfile.toLocal8Bit() );
 
129
  }
 
130
  else
 
131
  {
 
132
    if ( args->isSet( "outfile" ) )
 
133
    {
 
134
      kError() << "Error: --outfile specified without convert-to-native." << endl;
 
135
      return -1;
 
136
    }
 
137
    KigApplication app;
 
138
 
 
139
    // see if we are starting with session management
 
140
    if (app.isSessionRestored()) RESTORE(Kig)
 
141
      return app.exec();
 
142
  }
 
143
}