lib

KoApplication.cpp

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 <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 the *.desktop file corresponding to the kapp instance name
00095     KoDocumentEntry entry = KoDocumentEntry( KoDocument::readNativeService() );
00096     if ( entry.isEmpty() )
00097     {
00098         kdError( 30003 ) << instanceName() << "part.desktop not found." << endl;
00099         kdError( 30003 ) << "Run 'kde-config --path services' to see which directories were searched, assuming kde startup had the same environment as your current shell." << endl;
00100         kdError( 30003 ) << "Check your installation (did you install KOffice in a different prefix than KDE, without adding the prefix to /etc/kderc ?)" << endl;
00101         return false;
00102     }
00103 
00104     // Get the command line arguments which we have to parse
00105     KCmdLineArgs *args= KCmdLineArgs::parsedArgs();
00106     int argsCount = args->count();
00107 
00108     KCmdLineArgs *koargs = KCmdLineArgs::parsedArgs("koffice");
00109     QCString dpiValues = koargs->getOption( "dpi" );
00110     if ( !dpiValues.isEmpty() ) {
00111         int sep = dpiValues.find( QRegExp( "[x, ]" ) );
00112         int dpiX;
00113         int dpiY = 0;
00114         bool ok = true;
00115         if ( sep != -1 ) {
00116             dpiY = dpiValues.mid( sep+1 ).toInt( &ok );
00117             dpiValues.truncate( sep );
00118         }
00119         if ( ok ) {
00120             dpiX = dpiValues.toInt( &ok );
00121             if ( ok ) {
00122                 if ( !dpiY ) dpiY = dpiX;
00123                 KoGlobal::setDPI( dpiX, dpiY );
00124             }
00125         }
00126     }
00127 
00128     // No argument -> create an empty document
00129     if ( !argsCount ) {
00130         KoDocument* doc = entry.createDoc( 0, "Document" );
00131         if ( !doc )
00132             return false;
00133         KoMainWindow *shell = new KoMainWindow( doc->instance() );
00134         shell->show();
00135         QObject::connect(doc, SIGNAL(sigProgress(int)), shell, SLOT(slotProgress(int)));
00136         // for initDoc to fill in the recent docs list
00137         // and for KoDocument::slotStarted
00138         doc->addShell( shell );
00139 
00140         if ( doc->checkAutoSaveFile() ) {
00141           shell->setRootDocument( doc );
00142         } else {
00143           doc->showStartUpWidget( shell );
00144         }
00145 
00146         // FIXME This needs to be moved someplace else
00147     QObject::disconnect(doc, SIGNAL(sigProgress(int)), shell, SLOT(slotProgress(int)));
00148     } else {
00149         bool print = koargs->isSet("print");
00150     bool doTemplate = koargs->isSet("template");
00151         koargs->clear();
00152 
00153         // Loop through arguments
00154 
00155         short int n=0; // number of documents open
00156         short int nPrinted = 0;
00157         for(int i=0; i < argsCount; i++ )
00158         {
00159             // For now create an empty document
00160             KoDocument* doc = entry.createDoc( 0 );
00161             if ( doc )
00162             {
00163                 // show a shell asap
00164                 KoMainWindow *shell = new KoMainWindow( doc->instance() );
00165                 if (!print)
00166                     shell->show();
00167         // are we just trying to open a template?
00168         if ( doTemplate ) {
00169           QStringList paths;
00170           if ( args->url(i).isLocalFile() && QFile::exists(args->url(i).path()) )
00171           {
00172             paths << QString(args->url(i).path());
00173             kdDebug(30003) << "using full path..." << endl;
00174           } else {
00175              QString desktopName(args->arg(i));
00176              QString appName = KGlobal::instance()->instanceName();
00177 
00178              paths = KGlobal::dirs()->findAllResources("data", appName +"/templates/*/" + desktopName );
00179              if ( paths.isEmpty()) {
00180                paths = KGlobal::dirs()->findAllResources("data", appName +"/templates/" + desktopName );
00181                  }
00182              if ( paths.isEmpty()) {
00183                 KMessageBox::error(0L, i18n("No template found for: %1 ").arg(desktopName) );
00184                 delete shell;
00185              } else if ( paths.count() > 1 ) {
00186                 KMessageBox::error(0L,  i18n("Too many templates found for: %1").arg(desktopName) );
00187                 delete shell;
00188              }
00189           }
00190 
00191                   if ( !paths.isEmpty() ) {
00192              KURL templateBase;
00193              templateBase.setPath(paths[0]);
00194              KDesktopFile templateInfo(paths[0]);
00195 
00196              QString templateName = templateInfo.readURL();
00197              KURL templateURL;
00198              templateURL.setPath( templateBase.directory() + "/" + templateName );
00199              if ( shell->openDocument(doc, templateURL )) {
00200                doc->resetURL();
00201                doc->setEmpty();
00202                        doc->setTitleModified();
00203                kdDebug(30003) << "Template loaded..." << endl;
00204                n++;
00205              } else {
00206                 KMessageBox::error(0L, i18n("Template %1 failed to load.").arg(templateURL.prettyURL()) );
00207                 delete shell;
00208              }
00209           }
00210                 // now try to load
00211                 } else if ( shell->openDocument( doc, args->url(i) ) ) {
00212                     if ( print ) {
00213                         shell->print(false /*we want to get the dialog*/);
00214                         // delete shell; done by ~KoDocument
00215                         nPrinted++;
00216             } else {
00217                         // Normal case, success
00218                         n++;
00219                     }
00220                 } else {
00221                     // .... if failed
00222                     // delete doc; done by openDocument
00223                     // delete shell; done by ~KoDocument
00224                 }
00225             }
00226         }
00227         if ( print )
00228             return nPrinted > 0;
00229         if (n == 0) // no doc, e.g. all URLs were malformed
00230             return false;
00231     }
00232 
00233     args->clear();
00234     // not calling this before since the program will quit there.
00235     return true;
00236 }
00237 
00238 KoApplication::~KoApplication()
00239 {
00240     delete d->m_appIface;
00241     delete d;
00242 }
00243 
00244 bool KoApplication::isStarting()
00245 {
00246     return KoApplication::m_starting;
00247 }
00248 
00249 #include <KoApplication.moc>
KDE Home | KDE Accessibility Home | Description of Access Keys