KoApplication.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KoApplication.h"
21#include <config.h>
22#include <tqfile.h>
23#include <tqregexp.h>
24#include <dcopclient.h>
25#include <KoApplicationIface.h>
26#include <KoQueryTrader.h>
27#include <KoDocument.h>
28#include <KoMainWindow.h>
29#include <tdelocale.h>
30#include <tdecmdlineargs.h>
31#include <kdebug.h>
32#include <kdesktopfile.h>
33#include <tdemessagebox.h>
34#include <kstandarddirs.h>
35#include <stdlib.h>
36
37void tqt_generate_epsf( bool b );
38
39static const TDECmdLineOptions options[]=
40{
41 {"print", I18N_NOOP("Only print and exit"),0},
42 {"template", I18N_NOOP("Open a new document with a template"), 0},
43 {"dpi <dpiX,dpiY>", I18N_NOOP("Override display DPI"), 0},
44 TDECmdLineLastOption
45};
46
47bool KoApplication::m_starting = true;
48
49class KoApplicationPrivate
50{
51public:
52 KoApplicationPrivate() {
53 m_appIface = 0L;
54 }
55 KoApplicationIface *m_appIface; // to avoid a leak
56};
57
59 : TDEApplication( initHack() )
60{
61 d = new KoApplicationPrivate;
62
63 // Initialize all KOffice directories etc.
64 KoGlobal::initialize();
65
66 // Prepare a DCOP interface
67 d->m_appIface = new KoApplicationIface;
68 dcopClient()->setDefaultObject( d->m_appIface->objId() );
69
70 m_starting = true;
71}
72
73// This gets called before entering TDEApplication::TDEApplication
74bool KoApplication::initHack()
75{
76 TDECmdLineArgs::addCmdLineOptions( options, I18N_NOOP("KOffice"), "koffice", "kde" );
77 return true;
78}
79
80// Small helper for start() so that we don't forget to reset m_starting before a return
81class KoApplication::ResetStarting
82{
83public:
84 ~ResetStarting() {
85 KoApplication::m_starting = false;
86 }
87};
88
90{
91 ResetStarting resetStarting; // reset m_starting to false when we're done
92 Q_UNUSED( resetStarting );
93
94 // Find the *.desktop file corresponding to the kapp instance name
96 if ( entry.isEmpty() )
97 {
98 kdError( 30003 ) << instanceName() << "part.desktop not found." << endl;
99 kdError( 30003 ) << "Run 'tde-config --path services' to see which directories were searched, assuming kde startup had the same environment as your current shell." << endl;
100 kdError( 30003 ) << "Check your installation (did you install KOffice in a different prefix than TDE, without adding the prefix to /etc/tderc ?)" << endl;
101 return false;
102 }
103
104 // Get the command line arguments which we have to parse
105 TDECmdLineArgs *args= TDECmdLineArgs::parsedArgs();
106 int argsCount = args->count();
107
108 TDECmdLineArgs *koargs = TDECmdLineArgs::parsedArgs("koffice");
109 TQCString dpiValues = koargs->getOption( "dpi" );
110 if ( !dpiValues.isEmpty() ) {
111 int sep = dpiValues.find( TQRegExp( "[x, ]" ) );
112 int dpiX;
113 int dpiY = 0;
114 bool ok = true;
115 if ( sep != -1 ) {
116 dpiY = dpiValues.mid( sep+1 ).toInt( &ok );
117 dpiValues.truncate( sep );
118 }
119 if ( ok ) {
120 dpiX = dpiValues.toInt( &ok );
121 if ( ok ) {
122 if ( !dpiY ) dpiY = dpiX;
123 KoGlobal::setDPI( dpiX, dpiY );
124 }
125 }
126 }
127
128 // No argument -> create an empty document
129 if ( !argsCount ) {
130 KoDocument* doc = entry.createDoc( 0, "Document" );
131 if ( !doc )
132 return false;
133 KoMainWindow *shell = new KoMainWindow( doc->instance() );
134 shell->show();
135 TQObject::connect(doc, TQ_SIGNAL(sigProgress(int)), shell, TQ_SLOT(slotProgress(int)));
136 // for initDoc to fill in the recent docs list
137 // and for KoDocument::slotStarted
138 doc->addShell( shell );
139
140 if ( doc->checkAutoSaveFile() ) {
141 shell->setRootDocument( doc );
142 } else {
143 doc->showStartUpWidget( shell );
144 }
145
146 // FIXME This needs to be moved someplace else
147 TQObject::disconnect(doc, TQ_SIGNAL(sigProgress(int)), shell, TQ_SLOT(slotProgress(int)));
148 } else {
149 bool print = koargs->isSet("print");
150 bool doTemplate = koargs->isSet("template");
151 koargs->clear();
152
153 // Loop through arguments
154
155 short int n=0; // number of documents open
156 short int nPrinted = 0;
157 for(int i=0; i < argsCount; i++ )
158 {
159 // For now create an empty document
160 KoDocument* doc = entry.createDoc( 0 );
161 if ( doc )
162 {
163 // show a shell asap
164 KoMainWindow *shell = new KoMainWindow( doc->instance() );
165 if (!print)
166 shell->show();
167 // are we just trying to open a template?
168 if ( doTemplate ) {
169 TQStringList paths;
170 if ( args->url(i).isLocalFile() && TQFile::exists(args->url(i).path()) )
171 {
172 paths << TQString(args->url(i).path());
173 kdDebug(30003) << "using full path..." << endl;
174 } else {
175 TQString desktopName(args->arg(i));
176 TQString appName = TDEGlobal::instance()->instanceName();
177
178 paths = TDEGlobal::dirs()->findAllResources("data", appName +"/templates/*/" + desktopName );
179 if ( paths.isEmpty()) {
180 paths = TDEGlobal::dirs()->findAllResources("data", appName +"/templates/" + desktopName );
181 }
182 if ( paths.isEmpty()) {
183 KMessageBox::error(0L, i18n("No template found for: %1 ").arg(desktopName) );
184 delete shell;
185 } else if ( paths.count() > 1 ) {
186 KMessageBox::error(0L, i18n("Too many templates found for: %1").arg(desktopName) );
187 delete shell;
188 }
189 }
190
191 if ( !paths.isEmpty() ) {
192 KURL templateBase;
193 templateBase.setPath(paths[0]);
194 KDesktopFile templateInfo(paths[0]);
195
196 TQString templateName = templateInfo.readURL();
197 KURL templateURL;
198 templateURL.setPath( templateBase.directory() + "/" + templateName );
199 if ( shell->openDocument(doc, templateURL )) {
200 doc->resetURL();
201 doc->setEmpty();
202 doc->setTitleModified();
203 kdDebug(30003) << "Template loaded..." << endl;
204 n++;
205 } else {
206 KMessageBox::error(0L, i18n("Template %1 failed to load.").arg(templateURL.prettyURL()) );
207 delete shell;
208 }
209 }
210 // now try to load
211 } else if ( shell->openDocument( doc, args->url(i) ) ) {
212 if ( print ) {
213 shell->print(false /*we want to get the dialog*/);
214 // delete shell; done by ~KoDocument
215 nPrinted++;
216 } else {
217 // Normal case, success
218 n++;
219 }
220 } else {
221 // .... if failed
222 // delete doc; done by openDocument
223 // delete shell; done by ~KoDocument
224 }
225 }
226 }
227 if ( print )
228 return nPrinted > 0;
229 if (n == 0) // no doc, e.g. all URLs were malformed
230 return false;
231 }
232
233 args->clear();
234 // not calling this before since the program will quit there.
235 return true;
236}
237
239{
240 delete d->m_appIface;
241 delete d;
242}
243
245{
246 return KoApplication::m_starting;
247}
248
249#include <KoApplication.moc>
static void error(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
virtual TDEInstance * instance() const
DCOP interface for any KOffice application (entry point)
virtual ~KoApplication()
Destructor.
virtual bool start()
Call this to start the application.
KoApplication()
Creates an application object, adds some standard directories and initializes kimgio.
static bool isStarting()
Represents an available KOffice component that supports the document interface.
bool isEmpty() const
KoDocument * createDoc(KoDocument *parent=0, const char *name=0) const
Uses the factory of the component to create a document.
The KOffice document class.
Definition KoDocument.h:61
void resetURL()
Sets the document URL to empty URL KParts doesn't allow this, but KOffice apps have e....
Definition KoDocument.h:818
bool checkAutoSaveFile()
Called by koApplication to check for an autosave file in $HOME.
virtual void setEmpty()
Sets the document to empty.
Definition KoDocument.h:499
virtual void addShell(KoMainWindow *shell)
Appends the shell to the list of shells which show this document as their root document.
static KService::Ptr readNativeService(TDEInstance *instance=0)
With the help of instance or TDEApplication::instance() this method figures out which ....
virtual void showStartUpWidget(KoMainWindow *parent, bool alwaysShow=false)
Creates and shows the start up widget.
virtual void setTitleModified()
Tells the document that its title has been modified, either because the modified status changes (this...
Main window for a KOffice application.
void print(bool quick)
Prints the document.
virtual void setRootDocument(KoDocument *doc)
Called when a document is assigned to this mainwindow.
virtual bool openDocument(const KURL &url)
Load the desired document and show it.
virtual void show()