Exemple #1
0
bool ItemEditor::start()
{
    // create temp file
    const QString tmpFileName = QString("CopyQ.XXXXXX") + getFileSuffixFromMime(m_mime);
    QString tmpPath = QDir( QDir::tempPath() ).absoluteFilePath(tmpFileName);

    QTemporaryFile tmpfile;
    tmpfile.setFileTemplate(tmpPath);
    tmpfile.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner);

    if ( !tmpfile.open() ) {
        printError( tr("Failed to open temporary file (%1) for editing item in external editor!")
             .arg(tmpfile.fileName()) );
        return false;
    }

    // write text to temp file
    tmpfile.write(m_data);
    tmpfile.flush();

    // monitor file
    m_info.setFile( tmpfile.fileName() );
    m_lastmodified = m_info.lastModified();
    m_lastSize = m_info.size();
    m_timer->start(500);
    connect( m_timer, SIGNAL(timeout()),
             this, SLOT(onTimer()) );

    // create editor process
    m_editor = new QProcess(this);
    connect( m_editor, SIGNAL(finished(int, QProcess::ExitStatus)),
            this, SLOT(close()) );
    connect( m_editor, SIGNAL(error(QProcess::ProcessError)),
            this, SLOT(onError()) );

    // use native path for filename to edit and escape all backslashes
    const QString nativeFilePath = QDir::toNativeSeparators(m_info.filePath());
    QString cmd = m_editorcmd.arg('"' + nativeFilePath + '"');
    cmd.replace("\\", "\\\\");

    // execute editor
    m_editor->start(cmd);

    tmpfile.setAutoRemove(false);
    tmpfile.close();

    return true;
}
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);
}