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