koApplication.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "config.h"
00021 #include <qfile.h>
00022 #include <dcopclient.h>
00023 #include <koApplication.h>
00024 #include <KoApplicationIface.h>
00025 #include <koQueryTrader.h>
00026 #include <koDocument.h>
00027 #include <koMainWindow.h>
00028 #include <klocale.h>
00029 #include <kcmdlineargs.h>
00030 #include <kdebug.h>
00031 #include <kdesktopfile.h>
00032 #include <kmessagebox.h>
00033 #include <kstandarddirs.h>
00034 #include <stdlib.h>
00035
00036 void qt_generate_epsf( bool b );
00037
00038 static const KCmdLineOptions options[]=
00039 {
00040 {"print", I18N_NOOP("Only print and exit"),0},
00041 {"template", I18N_NOOP("Open a new document with a template"), 0},
00042 KCmdLineLastOption
00043 };
00044
00045 bool KoApplication::m_starting = true;
00046
00047 class KoApplicationPrivate
00048 {
00049 public:
00050 KoApplicationPrivate() {
00051 m_appIface = 0L;
00052 }
00053 KoApplicationIface *m_appIface;
00054 };
00055
00056 KoApplication::KoApplication()
00057 : KApplication( initHack() )
00058 {
00059 d = new KoApplicationPrivate;
00060
00061
00062 KoGlobal::initialize();
00063
00064
00065 d->m_appIface = new KoApplicationIface;
00066 dcopClient()->setDefaultObject( d->m_appIface->objId() );
00067
00068 m_starting = true;
00069 }
00070
00071
00072 bool KoApplication::initHack()
00073 {
00074 KCmdLineArgs::addCmdLineOptions( options, I18N_NOOP("KOffice"), "koffice", "kde" );
00075 return true;
00076 }
00077
00078
00079 class KoApplication::ResetStarting
00080 {
00081 public:
00082 ~ResetStarting() {
00083 KoApplication::m_starting = false;
00084 }
00085 };
00086
00087 bool KoApplication::start()
00088 {
00089 ResetStarting resetStarting;
00090 Q_UNUSED( resetStarting );
00091
00092
00093
00094 QCString nativeFormat = KoDocument::readNativeFormatMimeType();
00095 if ( nativeFormat.isEmpty() )
00096 {
00097 kdError(30003) << "Couldn't find the native MimeType in " << kapp->name() << "'s desktop file. Check your installation !" << endl;
00098 return false;
00099 }
00100
00101
00102 KoDocumentEntry entry = KoDocumentEntry::queryByMimeType( nativeFormat );
00103 if ( entry.isEmpty() )
00104 {
00105
00106 return false;
00107 }
00108
00109
00110 KCmdLineArgs *args= KCmdLineArgs::parsedArgs();
00111 int argsCount = args->count();
00112
00113
00114 if (!argsCount) {
00115 KoDocument* doc = entry.createDoc( 0, "Document" );
00116 if ( !doc )
00117 return false;
00118 KoMainWindow *shell = new KoMainWindow( doc->instance() );
00119 shell->show();
00120 QObject::connect(doc, SIGNAL(sigProgress(int)), shell, SLOT(slotProgress(int)));
00121
00122
00123 doc->addShell( shell );
00124
00125 doc->setInitDocFlags( KoDocument::InitDocAppStarting );
00126 if ( doc->checkAutoSaveFile() || doc->initDoc() )
00127 {
00128 shell->setRootDocument( doc );
00129 }
00130 else
00131 return false;
00132
00133 QObject::disconnect(doc, SIGNAL(sigProgress(int)), shell, SLOT(slotProgress(int)));
00134 } else {
00135 KCmdLineArgs *koargs = KCmdLineArgs::parsedArgs("koffice");
00136 bool print = koargs->isSet("print");
00137 bool doTemplate = koargs->isSet("template");
00138 koargs->clear();
00139
00140
00141
00142 short int n=0;
00143 for(int i=0; i < argsCount; i++ )
00144 {
00145
00146 KoDocument* doc = entry.createDoc( 0 );
00147 if ( doc )
00148 {
00149
00150 KoMainWindow *shell = new KoMainWindow( doc->instance() );
00151 if (!print)
00152 shell->show();
00153
00154 if ( doTemplate ) {
00155 QStringList paths;
00156 if ( args->url(i).isLocalFile() && QFile::exists(args->url(i).path()) )
00157 {
00158 paths << QString(args->url(i).path());
00159 kdDebug(3003) << "using full path..." << endl;
00160 } else {
00161 QString desktopName(args->arg(i));
00162 QString appName = KGlobal::instance()->instanceName();
00163
00164 paths = KGlobal::dirs()->findAllResources("data", appName +"/templates/*/" + desktopName );
00165 if ( paths.isEmpty()) {
00166 paths = KGlobal::dirs()->findAllResources("data", appName +"/templates/" + desktopName );
00167 }
00168 if ( paths.isEmpty()) {
00169 KMessageBox::error(0L, i18n("No template found for: %1 ").arg(desktopName) );
00170 delete shell;
00171 } else if ( paths.count() > 1 ) {
00172 KMessageBox::error(0L, i18n("Too many templates found for: %1").arg(desktopName) );
00173 delete shell;
00174 }
00175 }
00176
00177 if ( !paths.isEmpty() ) {
00178 KURL templateBase;
00179 templateBase.setPath(paths[0]);
00180 KDesktopFile templateInfo(paths[0]);
00181
00182 QString templateName = templateInfo.readURL();
00183 KURL templateURL;
00184 templateURL.setPath( templateBase.directory() + "/" + templateName );
00185 if ( shell->openDocument(doc, templateURL )) {
00186 doc->resetURL();
00187 doc->setEmpty();
00188 doc->setTitleModified();
00189 kdDebug(3003) << "Template loaded..." << endl;
00190 n++;
00191 } else {
00192 KMessageBox::error(0L, i18n("Template %1 failed to load.").arg(templateURL.prettyURL()) );
00193 delete shell;
00194 }
00195 }
00196
00197 } else if ( shell->openDocument( doc, args->url(i) ) ) {
00198 if ( print ) {
00199 shell->print(false );
00200
00201 } else {
00202
00203 n++;
00204 }
00205 } else {
00206
00207
00208
00209 }
00210 }
00211 }
00212 if (n == 0)
00213 return false;
00214 }
00215
00216 args->clear();
00217
00218 return true;
00219 }
00220
00221 KoApplication::~KoApplication()
00222 {
00223 delete d->m_appIface;
00224 delete d;
00225 }
00226
00227 bool KoApplication::isStarting()
00228 {
00229 return KoApplication::m_starting;
00230 }
00231
00232 #include <koApplication.moc>
This file is part of the documentation for lib Library Version 1.3.5.