summaryrefslogtreecommitdiffstats
path: root/kig/kig/main.cpp
blob: 8924a07d99d5336ba11fa6d0d8c475ba001f3ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 This file is part of Kig, a KDE program for Interactive Geometry...
 Copyright (C) 2002  Dominique Devriese <devriese@kde.org>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 USA
**/

#include "kig.h"

#include <kuniqueapplication.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <klocale.h>
#include <klibloader.h>
#include <kdebug.h>

#include "aboutdata.h"

static KCmdLineOptions options[] =
  {
    { "c", 0, 0 },
    { "convert-to-native", I18N_NOOP( "Do not show a GUI. Convert the specified file to the native Kig format. Output goes to stdout unless --outfile is specified." ), 0 },
    { "o", 0, 0 },
    { "outfile <file>", I18N_NOOP( "File to output the created native file to. '-' means output to stdout. Default is stdout as well." ), 0 },
    { "+[URL]", I18N_NOOP( "Document to open" ), 0 },
    KCmdLineLastOption
  };

class KigApplication
  : public KUniqueApplication
{
public:
  KigApplication( bool gui = true );
  int newInstance();
  void handleArgs( TDECmdLineArgs* args );
};

KigApplication::KigApplication( bool gui )
  : KUniqueApplication( gui, gui )
{
}

int KigApplication::newInstance()
{
  static bool first = true;
  if (isRestored() && first)
  {
    first = false;
    return 0;
  }
  first = false;

  TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
  handleArgs(args);
  args->clear();
  return 0;
}

void KigApplication::handleArgs( TDECmdLineArgs* args )
{
  if ( args->count() == 0 )
  {
    Kig *widget = new Kig;
    widget->show();
  }
  else
  {
    for (int i = 0; i < args->count(); i++ )
    {
      Kig *widget = new Kig;
      widget->show();
      widget->load( args->url( i ) );
    }
  }
}

static int convertToNative( const KURL& file, const TQCString& outfile )
{
  KigApplication app( false );
  KLibrary* library = KLibLoader::self()->globalLibrary( "libkigpart" );
  int ( *converterfunction )( const KURL&, const TQCString& );
  converterfunction = ( int ( * )( const KURL&, const TQCString& ) ) library->symbol( "convertToNative" );
  if ( !converterfunction )
  {
    kdError() << "Error: broken Kig installation: different library and application version !" << endl;
    return -1;
  }
  return (*converterfunction)( file, outfile );
}

int main(int argc, char **argv)
{
  KAboutData *about = kigAboutData( "kig", I18N_NOOP("Kig") );

  TDECmdLineArgs::init(argc, argv, about);
  TDECmdLineArgs::addCmdLineOptions( options );
  KigApplication::addCmdLineOptions();

  TDECmdLineArgs* args = TDECmdLineArgs::parsedArgs();
  if ( args->isSet( "convert-to-native" ) )
  {
    TQCString outfile = args->getOption( "outfile" );
    if ( outfile.isNull() )
      outfile = "-";

    if ( args->count() == 0 )
    {
      kdError() << "Error: --convert-to-native specified without a file to convert." << endl;
      return -1;
    }
    if ( args->count() > 1 )
    {
      kdError() << "Error: --convert-to-native specified with more than one file to convert." << endl;
      return -1;
    }
    return convertToNative( args->url( 0 ), outfile );
  }
  else
  {
    if ( args->isSet( "outfile" ) )
    {
      kdError() << "Error: --outfile specified without convert-to-native." << endl;
      return -1;
    }
    KigApplication app;

    // see if we are starting with session management
    if (app.isRestored()) RESTORE(Kig)
      return app.exec();
  }
}