File indexing completed on 2024-05-05 17:04:27

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2012 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "Settings.h"
0021 #include "DocumentListModel.h"
0022 
0023 #include <QApplication>
0024 #include <QUrl>
0025 #include <QMimeType>
0026 
0027 #include <KSharedConfig>
0028 #include <kconfiggroup.h>
0029 
0030 #include "Theme.h"
0031 #include "PropertyContainer.h"
0032 // #include <qtquick/CQTextDocumentCanvas.h>
0033 #include <KoDocumentEntry.h>
0034 #include <part/KWDocument.h>
0035 #include <stage/part/KPrDocument.h>
0036 
0037 class Settings::Private
0038 {
0039 public:
0040     Private() : temporaryFile(false), focusItem(0) { }
0041 
0042     QString currentFile;
0043     QString currentFileClass;
0044     bool temporaryFile;
0045     QQuickItem *focusItem;
0046     Theme* theme;
0047 };
0048 
0049 Settings::Settings( QObject* parent )
0050     : QObject( parent ), d( new Private )
0051 {
0052     QString theme = KSharedConfig::openConfig()->group("General").readEntry<QString>("theme", "default");
0053     d->theme = Theme::load(theme, this);
0054     connect(d->theme, SIGNAL(fontCacheRebuilt()), SIGNAL(themeChanged()));
0055 }
0056 
0057 Settings::~Settings()
0058 {
0059     delete d;
0060 }
0061 
0062 QString Settings::currentFile() const
0063 {
0064     return d->currentFile;
0065 }
0066 
0067 QString Settings::currentFileClass() const
0068 {
0069     return d->currentFileClass;
0070 }
0071 
0072 void Settings::setCurrentFile(const QString& fileName)
0073 {
0074     qApp->processEvents();
0075     if(fileName.isEmpty()) {
0076         d->currentFile = fileName;
0077         d->currentFileClass = "No document set, consequently no class. This is expected behaviour, do not report.";
0078         emit currentFileChanged();
0079     }
0080     else if (fileName != d->currentFile) {
0081         QUrl url(fileName);
0082         if(url.scheme() == "newfile") {
0083             d->currentFileClass = url.queryItemValue("mimetype");
0084         }
0085         else {
0086             QMimeDatabase db;
0087             QMimeType mimeType = db.mimeTypeForUrl(url);
0088             KoDocumentEntry documentEntry = KoDocumentEntry::queryByMimeType(mimeType.name());
0089             if(documentEntry.supportsMimeType(WORDS_MIME_TYPE)) {
0090                 d->currentFileClass = WORDS_MIME_TYPE;
0091             } else if(documentEntry.supportsMimeType(STAGE_MIME_TYPE)) {
0092                 d->currentFileClass = STAGE_MIME_TYPE;
0093             } else {
0094                 d->currentFileClass = QString("Unsupported document! Reported mimetype is %1").arg(mimeType.name());
0095             }
0096         }
0097         d->currentFile = fileName;
0098         emit currentFileChanged();
0099     }
0100 }
0101 
0102 bool Settings::isTemporaryFile() const
0103 {
0104     return d->temporaryFile;
0105 }
0106 
0107 void Settings::setTemporaryFile(bool temp)
0108 {
0109     if (temp != d->temporaryFile) {
0110         d->temporaryFile = temp;
0111         emit temporaryFileChanged();
0112     }
0113 }
0114 
0115 QQuickItem* Settings::focusItem()
0116 {
0117     return d->focusItem;
0118 }
0119 
0120 void Settings::setFocusItem(QQuickItem* item)
0121 {
0122     if (item != d->focusItem) {
0123         d->focusItem = item;
0124         emit focusItemChanged();
0125     }
0126 }
0127 
0128 QObject* Settings::theme() const
0129 {
0130     return d->theme;
0131 }
0132 
0133 QString Settings::themeID() const
0134 {
0135     if(d->theme)
0136         return d->theme->id();
0137 
0138     return QString();
0139 }
0140 
0141 void Settings::setThemeID(const QString& id)
0142 {
0143     if(!d->theme || id != d->theme->id()) {
0144         if(d->theme) {
0145             delete d->theme;
0146             d->theme = 0;
0147         }
0148 
0149         d->theme = Theme::load(id, this);
0150         KSharedConfig::openConfig()->group("General").writeEntry<QString>("theme", id);
0151 
0152         emit themeChanged();
0153     }
0154 }
0155 
0156 int Settings::mimeTypeToDocumentClass(QString mimeType) const
0157 {
0158     DocumentListModel::DocumentType documentClass = DocumentListModel::UnknownType;
0159     if(mimeType == QLatin1String("application/vnd.oasis.opendocument.text") ||
0160        mimeType == QLatin1String("application/msword") ||
0161        mimeType == QLatin1String("application/rtf") ||
0162        mimeType == QLatin1String("application/vnd.openxmlformats-officedocument.wordprocessingml.document") ||
0163        mimeType == QLatin1String("application/vnd.openxmlformats-officedocument.wordprocessingml.template") ||
0164        mimeType == QLatin1String("application/vnd.ms-word.document.macroEnabled.12") ||
0165        mimeType == QLatin1String("application/vnd.ms-word.template.macroEnabled.12"))
0166     {
0167         documentClass = DocumentListModel::TextDocumentType;
0168     }
0169     else
0170     if(mimeType == QLatin1String("application/vnd.oasis.opendocument.presentation") ||
0171        mimeType == QLatin1String("application/vnd.ms-powerpoint") ||
0172        mimeType == QLatin1String("application/vnd.openxmlformats-officedocument.presentationml.presentation") ||
0173        mimeType == QLatin1String("application/vnd.openxmlformats-officedocument.presentationml.template") ||
0174        mimeType == QLatin1String("application/vnd.openxmlformats-officedocument.presentationml.slideshow") ||
0175        mimeType == QLatin1String("application/vnd.ms-powerpoint.presentation.macroEnabled.12") ||
0176        mimeType == QLatin1String("application/vnd.ms-powerpoint.template.macroEnabled.12") ||
0177        mimeType == QLatin1String("application/vnd.ms-powerpoint.slideshow.macroEnabled.12") )
0178     {
0179         documentClass = DocumentListModel::PresentationType;
0180     }
0181 //     else
0182 //     if(mimeType == QLatin1String("application/vnd.oasis.opendocument.spreadsheet") ||
0183 //        mimeType == QLatin1String("application/vnd.ms-excel") ||
0184 //        mimeType == QLatin1String("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") ||
0185 //        mimeType == QLatin1String("application/vnd.openxmlformats-officedocument.spreadsheetml.template") ||
0186 //        mimeType == QLatin1String("application/vnd.ms-excel.sheet.macroEnabled") ||
0187 //        mimeType == QLatin1String("application/vnd.ms-excel.sheet.macroEnabled.12") ||
0188 //        mimeType == QLatin1String("application/vnd.ms-excel.template.macroEnabled.12") )
0189 //     {
0190 //         documentClass = DocumentListModel::SpreadSheetType;
0191 //     }
0192     return documentClass;
0193 }