lib
KoApplication.cpp00001
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 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
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
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
00137
00138 doc->addShell( shell );
00139
00140 if ( doc->checkAutoSaveFile() ) {
00141 shell->setRootDocument( doc );
00142 } else {
00143 doc->showStartUpWidget( shell );
00144 }
00145
00146
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
00154
00155 short int n=0;
00156 short int nPrinted = 0;
00157 for(int i=0; i < argsCount; i++ )
00158 {
00159
00160 KoDocument* doc = entry.createDoc( 0 );
00161 if ( doc )
00162 {
00163
00164 KoMainWindow *shell = new KoMainWindow( doc->instance() );
00165 if (!print)
00166 shell->show();
00167
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
00211 } else if ( shell->openDocument( doc, args->url(i) ) ) {
00212 if ( print ) {
00213 shell->print(false );
00214
00215 nPrinted++;
00216 } else {
00217
00218 n++;
00219 }
00220 } else {
00221
00222
00223
00224 }
00225 }
00226 }
00227 if ( print )
00228 return nPrinted > 0;
00229 if (n == 0)
00230 return false;
00231 }
00232
00233 args->clear();
00234
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>
|