File indexing completed on 2024-05-05 17:04:25
0001 /* 0002 * <one line to give the library's name and an idea of what it does.> 0003 * Copyright 2013 Dan Leinir Turthra Jensen <admin@leinir.dk> 0004 * 0005 * This program is free software; you can redistribute it and/or 0006 * modify it under the terms of the GNU General Public License as 0007 * published by the Free Software Foundation; either version 2 of 0008 * the License or (at your option) version 3 or any later version 0009 * accepted by the membership of KDE e.V. (or its successor approved 0010 * by the membership of KDE e.V.), which shall act as a proxy 0011 * defined in Section 14 of version 3 of the license. 0012 * 0013 * This program is distributed in the hope that it will be useful, 0014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0016 * GNU General Public License for more details. 0017 * 0018 * You should have received a copy of the GNU General Public License 0019 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0020 * 0021 */ 0022 0023 #include "desktopviewproxy.h" 0024 0025 #include <QProcess> 0026 #include <QDir> 0027 #include <QApplication> 0028 #include <QUrl> 0029 0030 #include <klocalizedstring.h> 0031 #include <krecentfilesaction.h> 0032 #include <kactioncollection.h> 0033 #include <KConfigGroup> 0034 #include <KSharedConfig> 0035 0036 #include <KoMainWindow.h> 0037 #include <KoFilterManager.h> 0038 #include <KoFileDialog.h> 0039 #include <KoDocumentEntry.h> 0040 #include <KoConfig.h> // CALLIGRA_OLD_PLUGIN_METADATA 0041 0042 #include "MainWindow.h" 0043 #include <DocumentManager.h> 0044 #include <RecentFileManager.h> 0045 #include <Settings.h> 0046 #include <KoDocument.h> 0047 #include <KWView.h> 0048 0049 class DesktopViewProxy::Private 0050 { 0051 public: 0052 Private(MainWindow* mainWindow, KoMainWindow* desktopView) 0053 : mainWindow(mainWindow) 0054 , desktopView(desktopView) 0055 , isImporting(false) 0056 {} 0057 MainWindow* mainWindow; 0058 KoMainWindow* desktopView; 0059 bool isImporting; 0060 }; 0061 0062 DesktopViewProxy::DesktopViewProxy(MainWindow* mainWindow, KoMainWindow* parent) 0063 : QObject(parent) 0064 , d(new Private(mainWindow, parent)) 0065 { 0066 Q_ASSERT(parent); // "There MUST be a KoMainWindow assigned, otherwise everything will blow up"); 0067 0068 // Hide this one... as it doesn't work at all well and release happens :P 0069 QAction* closeAction = d->desktopView->actionCollection()->action("file_close"); 0070 closeAction->setVisible(false); 0071 0072 // Concept is simple - simply steal all the actions we require to work differently, and reconnect them to local functions 0073 QAction* newAction = d->desktopView->actionCollection()->action("file_new"); 0074 newAction->disconnect(d->desktopView); 0075 connect(newAction, SIGNAL(triggered(bool)), this, SLOT(fileNew())); 0076 QAction* openAction = d->desktopView->actionCollection()->action("file_open"); 0077 openAction->disconnect(d->desktopView); 0078 connect(openAction, SIGNAL(triggered(bool)), this, SLOT(fileOpen())); 0079 QAction* saveAction = d->desktopView->actionCollection()->action("file_save"); 0080 saveAction->disconnect(d->desktopView); 0081 connect(saveAction, SIGNAL(triggered(bool)), this, SLOT(fileSave())); 0082 QAction* saveasAction = d->desktopView->actionCollection()->action("file_save_as"); 0083 saveasAction->disconnect(d->desktopView); 0084 connect(saveasAction, SIGNAL(triggered(bool)), this, SLOT(fileSaveAs())); 0085 QAction* reloadAction = d->desktopView->actionCollection()->action("file_reload_file"); 0086 reloadAction->disconnect(d->desktopView); 0087 connect(reloadAction, SIGNAL(triggered(bool)), this, SLOT(reload())); 0088 QAction* loadExistingAsNewAction = d->desktopView->actionCollection()->action("file_import_file"); 0089 loadExistingAsNewAction->disconnect(d->desktopView); 0090 connect(loadExistingAsNewAction, SIGNAL(triggered(bool)), this, SLOT(loadExistingAsNew())); 0091 0092 // Recent files need a touch more work, as they aren't simply an action. 0093 KRecentFilesAction* recent = qobject_cast<KRecentFilesAction*>(d->desktopView->actionCollection()->action("file_open_recent")); 0094 recent->disconnect(d->desktopView); 0095 connect(recent, SIGNAL(urlSelected(QUrl)), this, SLOT(slotFileOpenRecent(QUrl))); 0096 recent->clear(); 0097 recent->loadEntries(KSharedConfig::openConfig()->group("RecentFiles")); 0098 0099 connect(d->desktopView, SIGNAL(documentSaved()), this, SIGNAL(documentSaved())); 0100 } 0101 0102 DesktopViewProxy::~DesktopViewProxy() 0103 { 0104 delete d; 0105 } 0106 0107 void DesktopViewProxy::fileNew() 0108 { 0109 QProcess::startDetached(qApp->applicationFilePath(), QStringList(), QDir::currentPath()); 0110 } 0111 0112 void DesktopViewProxy::fileOpen() 0113 { 0114 QStringList mimeFilter; 0115 KoDocumentEntry entry = KoDocumentEntry::queryByMimeType(DocumentManager::instance()->settingsManager()->currentFileClass().toLatin1()); 0116 if (!entry.isEmpty()) { 0117 QJsonObject json = entry.metaData(); 0118 #ifdef CALLIGRA_OLD_PLUGIN_METADATA 0119 QStringList mimeTypes = json.value("X-KDE-ExtraNativeMimeTypes").toString().split(','); 0120 #else 0121 QStringList mimeTypes = json.value("X-KDE-ExtraNativeMimeTypes").toVariant().toStringList(); 0122 #endif 0123 0124 mimeFilter << KoFilterManager::mimeFilter(DocumentManager::instance()->settingsManager()->currentFileClass().toLatin1(), 0125 KoFilterManager::Import, 0126 mimeTypes); 0127 } 0128 0129 KoFileDialog dialog(d->desktopView, KoFileDialog::OpenFile, "OpenDocument"); 0130 dialog.setCaption(i18n("Open Document")); 0131 dialog.setDefaultDir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)); 0132 dialog.setMimeTypeFilters(mimeFilter); 0133 QString filename = dialog.filename(); 0134 if (filename.isEmpty()) return; 0135 0136 DocumentManager::instance()->recentFileManager()->addRecent(filename); 0137 0138 QProcess::startDetached(qApp->applicationFilePath(), QStringList() << filename, QDir::currentPath()); 0139 } 0140 0141 void DesktopViewProxy::fileSave() 0142 { 0143 if(DocumentManager::instance()->isTemporaryFile()) { 0144 if(d->desktopView->saveDocument(true)) { 0145 if (KoDocument* document = DocumentManager::instance()->document()) { 0146 DocumentManager::instance()->recentFileManager()->addRecent(document->url().toLocalFile()); 0147 DocumentManager::instance()->settingsManager()->setCurrentFile(document->url().toLocalFile()); 0148 DocumentManager::instance()->setTemporaryFile(false); 0149 emit documentSaved(); 0150 } 0151 } 0152 } else { 0153 DocumentManager::instance()->save(); 0154 emit documentSaved(); 0155 } 0156 } 0157 0158 bool DesktopViewProxy::fileSaveAs() 0159 { 0160 KoDocument* document = DocumentManager::instance()->document(); 0161 if (!document) 0162 return false; 0163 0164 if(d->desktopView->saveDocument(true)) { 0165 DocumentManager::instance()->recentFileManager()->addRecent(document->url().toLocalFile()); 0166 DocumentManager::instance()->settingsManager()->setCurrentFile(document->url().toLocalFile()); 0167 DocumentManager::instance()->setTemporaryFile(false); 0168 emit documentSaved(); 0169 return true; 0170 } 0171 0172 DocumentManager::instance()->settingsManager()->setCurrentFile(document->url().toLocalFile()); 0173 return false; 0174 } 0175 0176 void DesktopViewProxy::reload() 0177 { 0178 DocumentManager::instance()->reload(); 0179 } 0180 0181 void DesktopViewProxy::loadExistingAsNew() 0182 { 0183 d->isImporting = true; 0184 fileOpen(); 0185 d->isImporting = false; 0186 } 0187 0188 void DesktopViewProxy::slotFileOpenRecent(const QUrl& url) 0189 { 0190 QProcess::startDetached(qApp->applicationFilePath(), QStringList() << url.toLocalFile(), QDir::currentPath()); 0191 }