00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00027
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
00056 }
00057
00058 };
00059
00060 KoApplication::KoApplication()
00061 : KApplication( initHack() )
00062 {
00063 d = new KoApplicationPrivate;
00064
00065
00066 KoGlobal::initialize();
00067
00068
00069
00070
00071
00072 new KoApplicationAdaptor(this);
00073 QDBusConnection::sessionBus().registerObject("/application", this);
00074
00075 m_starting = true;
00076 }
00077
00078
00079 bool KoApplication::initHack()
00080 {
00081 KCmdLineArgs::addCmdLineOptions( options, I18N_NOOP("KOffice"), "koffice", "kde" );
00082 return true;
00083 }
00084
00085
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;
00097 Q_UNUSED( resetStarting );
00098
00099
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
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
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
00146
00147 doc->addShell( shell );
00148
00149 if ( doc->checkAutoSaveFile() ) {
00150 shell->setRootDocument( doc );
00151 } else {
00152 doc->showStartUpWidget( shell );
00153 }
00154
00155
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
00163
00164 short int n=0;
00165 short int nPrinted = 0;
00166 for(int i=0; i < argsCount; i++ )
00167 {
00168
00169 QString errorMsg;
00170 KoDocument* doc = entry.createDoc( &errorMsg, 0 );
00171 if ( doc )
00172 {
00173
00174 KoMainWindow *shell = new KoMainWindow( doc->instance() );
00175 if (!print)
00176 shell->show();
00177
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
00221 } else if ( shell->openDocument( doc, args->url(i) ) ) {
00222 if ( print ) {
00223 shell->print(false );
00224
00225 nPrinted++;
00226 } else {
00227
00228 n++;
00229 }
00230 } else {
00231
00232
00233
00234 }
00235 }
00236 }
00237 if ( print )
00238 return nPrinted > 0;
00239 if (n == 0)
00240 return false;
00241 }
00242
00243 args->clear();
00244
00245 return true;
00246 }
00247
00248 KoApplication::~KoApplication()
00249 {
00250
00251 delete d;
00252 }
00253
00254 bool KoApplication::isStarting()
00255 {
00256 return KoApplication::m_starting;
00257 }
00258
00259 #include <KoApplication.moc>