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 "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;  // to avoid a leak
00054 };
00055 
00056 KoApplication::KoApplication()
00057         : KApplication( initHack() )
00058 {
00059     d = new KoApplicationPrivate;
00060 
00061     // Initialize all KOffice directories etc.
00062     KoGlobal::initialize();
00063 
00064     // Prepare a DCOP interface
00065     d->m_appIface = new KoApplicationIface;
00066     dcopClient()->setDefaultObject( d->m_appIface->objId() );
00067 
00068     m_starting = true;
00069 }
00070 
00071 // This gets called before entering KApplication::KApplication
00072 bool KoApplication::initHack()
00073 {
00074     KCmdLineArgs::addCmdLineOptions( options, I18N_NOOP("KOffice"), "koffice", "kde" );
00075     return true;
00076 }
00077 
00078 // Small helper for start() so that we don't forget to reset m_starting before a return
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; // reset m_starting to false when we're done
00090     Q_UNUSED( resetStarting );
00091 
00092     // Find out about the mimetype which is natively supported
00093     // by this application.
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     // Find the *.desktop file corresponding to the mime type
00102     KoDocumentEntry entry = KoDocumentEntry::queryByMimeType( nativeFormat );
00103     if ( entry.isEmpty() )
00104     {
00105         // Error message already shown by queryByMimeType
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     // No argument -> create an empty document
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         // for initDoc to fill in the recent docs list
00122         // and for KoDocument::slotStarted
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         // Loop through arguments
00141 
00142         short int n=0;
00143         for(int i=0; i < argsCount; i++ )
00144         {
00145             // For now create an empty document
00146             KoDocument* doc = entry.createDoc( 0 );
00147             if ( doc )
00148             {
00149                 // show a shell asap
00150                 KoMainWindow *shell = new KoMainWindow( doc->instance() );
00151                 if (!print)
00152                     shell->show();
00153         // are we just trying to open a template?
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                 // now try to load
00197                 } else if ( shell->openDocument( doc, args->url(i) ) ) {
00198                     if ( print ) {
00199                         shell->print(false /*we want to get the dialog*/);
00200                         // delete shell; done by ~KoDocument
00201             } else {
00202                         // Normal case, success
00203                         n++;
00204                     }
00205                 } else {
00206                     // .... if failed
00207                     // delete doc; done by openDocument
00208                     // delete shell; done by ~KoDocument
00209                 }
00210             }
00211         }
00212         if (n == 0) // no doc, all URLs were malformed
00213           return false;
00214     }
00215 
00216     args->clear();
00217     // not calling this before since the program will quit there.
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>
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Mar 20 14:25:24 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003