void KoVersionDialog::slotOpen() { if (!list->currentItem()) return; KoVersionInfo *version = 0; for (int i = 0; i < m_doc->versionList().size(); ++i) { if (m_doc->versionList().at(i).date.toString() == list->currentItem()->text(0)) { version = &m_doc->versionList()[i]; break; } } if (!version) return; QTemporaryFile tmp; tmp.setAutoRemove(false); tmp.open(); tmp.write(version->data); tmp.flush(); tmp.setPermissions(QFile::ReadUser); tmp.flush(); if (!m_doc->documentPart()->mainWindows().isEmpty()) { //open the version in a new window if possible KoDocumentEntry entry = KoDocumentEntry::queryByMimeType(m_doc->nativeOasisMimeType()); if (entry.isEmpty()) { entry = KoDocumentEntry::queryByMimeType(m_doc->nativeFormatMimeType()); } Q_ASSERT(!entry.isEmpty()); QString errorMsg; KoPart *part= entry.createKoPart(&errorMsg); if (!part) { if (!errorMsg.isEmpty()) KMessageBox::error(0, errorMsg); return; } KoMainWindow *mainWindow = part->createMainWindow(); mainWindow ->openDocument(QUrl::fromLocalFile(tmp.fileName())); mainWindow ->show(); } else { m_doc->openUrl(QUrl::fromUserInput(tmp.fileName())); } tmp.setAutoRemove(true); slotButtonClicked(Close); }
bool KoFormulaShape::loadEmbeddedDocument( KoStore *store, const KoXmlElement &objectElement, const KoOdfLoadingContext &odfLoadingContext) { if ( !objectElement.hasAttributeNS( KoXmlNS::xlink, "href" ) ) { kError() << "Object element has no valid xlink:href attribute"; return false; } QString url = objectElement.attributeNS( KoXmlNS::xlink, "href" ); // It can happen that the url is empty e.g. when it is a // presentation:placeholder. if ( url.isEmpty() ) { return true; } QString tmpURL; if ( url[0] == '#' ) url = url.mid( 1 ); #define INTERNAL_PROTOCOL "intern" #define STORE_PROTOCOL "tar" if (KUrl::isRelativeUrl( url )) { if ( url.startsWith( "./" ) ) tmpURL = QString( INTERNAL_PROTOCOL ) + ":/" + url.mid( 2 ); else tmpURL = QString( INTERNAL_PROTOCOL ) + ":/" + url; } else tmpURL = url; QString path = tmpURL; if ( tmpURL.startsWith( INTERNAL_PROTOCOL ) ) { path = store->currentDirectory(); if ( !path.isEmpty() && !path.endsWith( '/' ) ) path += '/'; QString relPath = KUrl( tmpURL ).path(); path += relPath.mid( 1 ); // remove leading '/' } if ( !path.endsWith( '/' ) ) path += '/'; const QString mimeType = odfLoadingContext.mimeTypeForPath( path ); //kDebug(35001) << "path for manifest file=" << path << "mimeType=" << mimeType; if ( mimeType.isEmpty() ) { //kDebug(35001) << "Manifest doesn't have media-type for" << path; return false; } const bool isOdf = mimeType.startsWith( "application/vnd.oasis.opendocument" ); if ( !isOdf ) { tmpURL += "/maindoc.xml"; //kDebug(35001) << "tmpURL adjusted to" << tmpURL; } //kDebug(35001) << "tmpURL=" << tmpURL; QString errorMsg; KoDocumentEntry e = KoDocumentEntry::queryByMimeType( mimeType ); if ( e.isEmpty() ) { return false; } bool res = true; if ( tmpURL.startsWith( STORE_PROTOCOL ) || tmpURL.startsWith( INTERNAL_PROTOCOL ) || KUrl::isRelativeUrl( tmpURL ) ) { if ( isOdf ) { store->pushDirectory(); Q_ASSERT( tmpURL.startsWith( INTERNAL_PROTOCOL ) ); QString relPath = KUrl( tmpURL ).path().mid( 1 ); store->enterDirectory( relPath ); res = m_document->loadOasisFromStore( store ); store->popDirectory(); } else { if ( tmpURL.startsWith( INTERNAL_PROTOCOL ) ) tmpURL = KUrl( tmpURL ).path().mid( 1 ); res = m_document->loadFromStore( store, tmpURL ); } m_document->setStoreInternal( true ); } else { // Reference to an external document. Hmmm... m_document->setStoreInternal( false ); KUrl url( tmpURL ); if ( !url.isLocalFile() ) { //QApplication::restoreOverrideCursor(); // For security reasons we need to ask confirmation if the // url is remote. int result = KMessageBox::warningYesNoCancel( 0, i18n( "This document contains an external link to a remote document\n%1", tmpURL ), i18n( "Confirmation Required" ), KGuiItem( i18n( "Download" ) ), KGuiItem( i18n( "Skip" ) ) ); if ( result == KMessageBox::Cancel ) { //d->m_parent->setErrorMessage("USER_CANCELED"); return false; } if ( result == KMessageBox::Yes ) res = m_document->openUrl( url ); // and if == No, res will still be false so we'll use a kounavail below } else res = m_document->openUrl( url ); } if ( !res ) { QString errorMessage = m_document->errorMessage(); return false; } tmpURL.clear(); return res; }
bool KoApplication::start() { ResetStarting resetStarting; // reset m_starting to false when we're done Q_UNUSED(resetStarting); // Find the *.desktop file corresponding to the kapp instance name KoDocumentEntry entry = KoDocumentEntry(KoDocument::readNativeService()); if (entry.isEmpty()) { kError(30003) << KGlobal::mainComponent().componentName() << "part.desktop not found." << endl; 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; kError(30003) << "Check your installation (did you install KOffice in a different prefix than KDE, without adding the prefix to /etc/kderc ?)" << endl; return false; } // Get the command line arguments which we have to parse KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); int argsCount = args->count(); KCmdLineArgs *koargs = KCmdLineArgs::parsedArgs("koffice"); QString dpiValues = koargs->getOption("dpi"); if (!dpiValues.isEmpty()) { int sep = dpiValues.indexOf(QRegExp("[x, ]")); int dpiX; int dpiY = 0; bool ok = true; if (sep != -1) { dpiY = dpiValues.mid(sep + 1).toInt(&ok); dpiValues.truncate(sep); } if (ok) { dpiX = dpiValues.toInt(&ok); if (ok) { if (!dpiY) dpiY = dpiX; KoGlobal::setDPI(dpiX, dpiY); } } } // No argument -> create an empty document if (!argsCount) { QString errorMsg; KoDocument* doc = entry.createDoc(&errorMsg); if (!doc) { if (!errorMsg.isEmpty()) KMessageBox::error(0, errorMsg); return false; } KoMainWindow *shell = new KoMainWindow(doc->componentData()); shell->show(); QObject::connect(doc, SIGNAL(sigProgress(int)), shell, SLOT(slotProgress(int))); // for initDoc to fill in the recent docs list // and for KoDocument::slotStarted doc->addShell(shell); if (doc->checkAutoSaveFile()) { shell->setRootDocument(doc); } else { doc->showStartUpWidget(shell); } // FIXME This needs to be moved someplace else QObject::disconnect(doc, SIGNAL(sigProgress(int)), shell, SLOT(slotProgress(int))); } else { const bool print = koargs->isSet("print"); const bool exportAsPdf = koargs->isSet("export-pdf"); QString pdfFileName = koargs->getOption("export-filename"); const bool doTemplate = koargs->isSet("template"); koargs->clear(); // Loop through arguments short int n = 0; // number of documents open short int nPrinted = 0; for (int i = 0; i < argsCount; i++) { // For now create an empty document QString errorMsg; KoDocument* doc = entry.createDoc(&errorMsg, 0); if (doc) { // show a shell asap KoMainWindow *shell = new KoMainWindow(doc->componentData()); shell->show(); // are we just trying to open a template? if (doTemplate) { QStringList paths; if (args->url(i).isLocalFile() && QFile::exists(args->url(i).toLocalFile())) { paths << QString(args->url(i).toLocalFile()); kDebug(30003) << "using full path..."; } else { QString desktopName(args->arg(i)); QString appName = KGlobal::mainComponent().componentName(); paths = KGlobal::dirs()->findAllResources("data", appName + "/templates/*/" + desktopName); if (paths.isEmpty()) { paths = KGlobal::dirs()->findAllResources("data", appName + "/templates/" + desktopName); } if (paths.isEmpty()) { KMessageBox::error(0L, i18n("No template found for: %1 ", desktopName)); delete shell; } else if (paths.count() > 1) { KMessageBox::error(0L, i18n("Too many templates found for: %1", desktopName)); delete shell; } } if (!paths.isEmpty()) { KUrl templateBase; templateBase.setPath(paths[0]); KDesktopFile templateInfo(paths[0]); QString templateName = templateInfo.readUrl(); KUrl templateURL; templateURL.setPath(templateBase.directory() + '/' + templateName); if (shell->openDocument(doc, templateURL)) { doc->resetURL(); doc->setEmpty(); doc->setTitleModified(); kDebug(30003) << "Template loaded..."; n++; } else { KMessageBox::error(0L, i18n("Template %1 failed to load.", templateURL.prettyUrl())); delete shell; } } // now try to load } else if (shell->openDocument(doc, args->url(i))) { if (print) { shell->slotFilePrint(); // delete shell; done by ~KoDocument nPrinted++; } else if (exportAsPdf) { KoPrintJob *job = shell->exportToPdf(pdfFileName); if (job) connect (job, SIGNAL(destroyed(QObject*)), shell, SLOT(slotFileQuit()), Qt::QueuedConnection); nPrinted++; } else { // Normal case, success n++; } } else { // .... if failed // delete doc; done by openDocument // delete shell; done by ~KoDocument } } } if (print || exportAsPdf) return nPrinted > 0; if (n == 0) // no doc, e.g. all URLs were malformed return false; } args->clear(); // not calling this before since the program will quit there. return true; }