F:/KPlato/koffice/libs/kofficecore/KoApplication.cpp

Aller à la documentation de ce fichier.
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., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "KoApplication.h"
00021 #include "KoApplicationAdaptor.h"
00022 #include <QtDBus/QtDBus>
00023 #include <config.h>
00024 #include <QFile>
00025 #include <QRegExp>
00026 // #include <dcopclient.h>
00027 // #include <KoApplicationIface.h>
00028 #include <KoQueryTrader.h>
00029 #include <KoDocument.h>
00030 #include <KoMainWindow.h>
00031 #include <klocale.h>
00032 #include <kcmdlineargs.h>
00033 #include <kdebug.h>
00034 #include <kdesktopfile.h>
00035 #include <kmessagebox.h>
00036 #include <kstandarddirs.h>
00037 #include <stdlib.h>
00038 
00039 void qt_generate_epsf( bool b );
00040 
00041 static const KCmdLineOptions options[]=
00042 {
00043         {"print", I18N_NOOP("Only print and exit"),0},
00044         {"template", I18N_NOOP("Open a new document with a template"), 0},
00045         {"dpi <dpiX,dpiY>", I18N_NOOP("Override display DPI"), 0},
00046         KCmdLineLastOption
00047 };
00048 
00049 bool KoApplication::m_starting = true;
00050 
00051 class KoApplicationPrivate
00052 {
00053 public:
00054     KoApplicationPrivate()  {
00055 //         m_appIface = 0L;
00056     }
00057 //     KoApplicationIface *m_appIface;  // to avoid a leak
00058 };
00059 
00060 KoApplication::KoApplication()
00061         : KApplication( initHack() )
00062 {
00063     d = new KoApplicationPrivate;
00064 
00065     // Initialize all KOffice directories etc.
00066     KoGlobal::initialize();
00067 
00068     // Prepare a DCOP interface
00069 //     d->m_appIface = new KoApplicationIface;
00070 //     dcopClient()->setDefaultObject( d->m_appIface->objId() );
00071 
00072     new KoApplicationAdaptor(this);
00073     QDBusConnection::sessionBus().registerObject("/application", this);
00074 
00075     m_starting = true;
00076 }
00077 
00078 // This gets called before entering KApplication::KApplication
00079 bool KoApplication::initHack()
00080 {
00081     KCmdLineArgs::addCmdLineOptions( options, I18N_NOOP("KOffice"), "koffice", "kde" );
00082     return true;
00083 }
00084 
00085 // Small helper for start() so that we don't forget to reset m_starting before a return
00086 class KoApplication::ResetStarting
00087 {
00088 public:
00089     ~ResetStarting()  {
00090         KoApplication::m_starting = false;
00091     }
00092 };
00093 
00094 bool KoApplication::start()
00095 {
00096     ResetStarting resetStarting; // reset m_starting to false when we're done
00097     Q_UNUSED( resetStarting );
00098 
00099     // Find the *.desktop file corresponding to the kapp instance name
00100     KoDocumentEntry entry = KoDocumentEntry( KoDocument::readNativeService() );
00101     if ( entry.isEmpty() )
00102     {
00103         kError( 30003 ) << instanceName() << "part.desktop not found." << endl;
00104         kError( 30003 ) << "Run 'kde4-config --path services' to see which directories were searched, assuming kde startup had the same environment as your current shell." << endl;
00105         kError( 30003 ) << "Check your installation (did you install KOffice in a different prefix than KDE, without adding the prefix to /etc/kderc ?)" << endl;
00106         return false;
00107     }
00108 
00109     // Get the command line arguments which we have to parse
00110     KCmdLineArgs *args= KCmdLineArgs::parsedArgs();
00111     int argsCount = args->count();
00112 
00113     KCmdLineArgs *koargs = KCmdLineArgs::parsedArgs("koffice");
00114     QString dpiValues = QString::fromLatin1( koargs->getOption( "dpi" ) );
00115     if ( !dpiValues.isEmpty() ) {
00116         int sep = dpiValues.indexOf( QRegExp( "[x, ]" ) );
00117         int dpiX;
00118         int dpiY = 0;
00119         bool ok = true;
00120         if ( sep != -1 ) {
00121             dpiY = dpiValues.mid( sep+1 ).toInt( &ok );
00122             dpiValues.truncate( sep );
00123         }
00124         if ( ok ) {
00125             dpiX = dpiValues.toInt( &ok );
00126             if ( ok ) {
00127                 if ( !dpiY ) dpiY = dpiX;
00128                 KoGlobal::setDPI( dpiX, dpiY );
00129             }
00130         }
00131     }
00132 
00133     // No argument -> create an empty document
00134     if ( !argsCount ) {
00135         QString errorMsg;
00136         KoDocument* doc = entry.createDoc( &errorMsg );
00137         if ( !doc ) {
00138             if ( !errorMsg.isEmpty() )
00139                 KMessageBox::error( 0, errorMsg );
00140             return false;
00141         }
00142         KoMainWindow *shell = new KoMainWindow( doc->instance() );
00143         shell->show();
00144         QObject::connect(doc, SIGNAL(sigProgress(int)), shell, SLOT(slotProgress(int)));
00145         // for initDoc to fill in the recent docs list
00146         // and for KoDocument::slotStarted
00147         doc->addShell( shell );
00148 
00149         if ( doc->checkAutoSaveFile() ) {
00150           shell->setRootDocument( doc );
00151         } else {
00152           doc->showStartUpWidget( shell );
00153         }
00154 
00155         // FIXME This needs to be moved someplace else
00156         QObject::disconnect(doc, SIGNAL(sigProgress(int)), shell, SLOT(slotProgress(int)));
00157     } else {
00158         bool print = koargs->isSet("print");
00159         bool doTemplate = koargs->isSet("template");
00160         koargs->clear();
00161 
00162         // Loop through arguments
00163 
00164         short int n=0; // number of documents open
00165         short int nPrinted = 0;
00166         for(int i=0; i < argsCount; i++ )
00167         {
00168             // For now create an empty document
00169             QString errorMsg;
00170             KoDocument* doc = entry.createDoc( &errorMsg, 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                     kDebug(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 ", desktopName) );
00194                         delete shell;
00195                      } else if ( paths.count() > 1 ) {
00196                         KMessageBox::error(0L,  i18n("Too many templates found for: %1", 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                        kDebug(30003) << "Template loaded..." << endl;
00214                        n++;
00215                      } else {
00216                         KMessageBox::error(0L, i18n("Template %1 failed to load.", 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>

Généré le Wed Nov 22 23:41:01 2006 pour KPlato par  doxygen 1.5.1-p1