lib Library API Documentation

koApplication.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
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;  // to avoid a leak
00056 };
00057 
00058 KoApplication::KoApplication()
00059         : KApplication( initHack() )
00060 {
00061     d = new KoApplicationPrivate;
00062 
00063     // Initialize all KOffice directories etc.
00064     KoGlobal::initialize();
00065 
00066     // Prepare a DCOP interface
00067     d->m_appIface = new KoApplicationIface;
00068     dcopClient()->setDefaultObject( d->m_appIface->objId() );
00069 
00070     m_starting = true;
00071 }
00072 
00073 // This gets called before entering KApplication::KApplication
00074 bool KoApplication::initHack()
00075 {
00076     KCmdLineArgs::addCmdLineOptions( options, I18N_NOOP("KOffice"), "koffice", "kde" );
00077     return true;
00078 }
00079 
00080 // Small helper for start() so that we don't forget to reset m_starting before a return
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; // reset m_starting to false when we're done
00092     Q_UNUSED( resetStarting );
00093 
00094     // Find out about the mimetype which is natively supported
00095     // by this application.
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     // Find the *.desktop file corresponding to the mime type
00104     KoDocumentEntry entry = KoDocumentEntry::queryByMimeType( nativeFormat );
00105     if ( entry.isEmpty() )
00106     {
00107         // Error message already shown by queryByMimeType
00108         return false;
00109     }
00110 
00111     // Get the command line arguments which we have to parse
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     // No argument -> create an empty document
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         // for initDoc to fill in the recent docs list
00144         // and for KoDocument::slotStarted
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         // Loop through arguments
00164 
00165         short int n=0; // number of documents open
00166         short int nPrinted = 0;
00167         for(int i=0; i < argsCount; i++ )
00168         {
00169             // For now create an empty document
00170             KoDocument* doc = entry.createDoc( 0 );
00171             if ( doc )
00172             {
00173                 // show a shell asap
00174                 KoMainWindow *shell = new KoMainWindow( doc->instance() );
00175                 if (!print)
00176                     shell->show();
00177         // are we just trying to open a template?
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                 // now try to load
00221                 } else if ( shell->openDocument( doc, args->url(i) ) ) {
00222                     if ( print ) {
00223                         shell->print(false /*we want to get the dialog*/);
00224                         // delete shell; done by ~KoDocument
00225                         nPrinted++;
00226             } else {
00227                         // Normal case, success
00228                         n++;
00229                     }
00230                 } else {
00231                     // .... if failed
00232                     // delete doc; done by openDocument
00233                     // delete shell; done by ~KoDocument
00234                 }
00235             }
00236         }
00237         if ( print )
00238             return nPrinted > 0;
00239         if (n == 0) // no doc, e.g. all URLs were malformed
00240             return false;
00241     }
00242 
00243     args->clear();
00244     // not calling this before since the program will quit there.
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>
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:39:57 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003