File indexing completed on 2024-04-28 05:08:17
0001 /*************************************************************************** 0002 Copyright (C) 2004-2009 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #include "dbusinterface.h" 0026 #include "controller.h" 0027 #include "tellico_kernel.h" 0028 #include "document.h" 0029 #include "collection.h" 0030 #include "fieldformat.h" 0031 #include "utils/bibtexhandler.h" 0032 #include "mainwindow.h" 0033 0034 #include <QDBusConnection> 0035 0036 using Tellico::ApplicationInterface; 0037 using Tellico::CollectionInterface; 0038 0039 ApplicationInterface::ApplicationInterface(Tellico::MainWindow* parent_) : QObject(parent_), m_mainWindow(parent_) { 0040 QDBusConnection::sessionBus().registerObject(QStringLiteral("/Tellico"), this, QDBusConnection::ExportScriptableSlots); 0041 } 0042 0043 Tellico::Import::Action ApplicationInterface::actionType(const QString& actionName) { 0044 QString name = actionName.toLower(); 0045 if(name == QLatin1String("append")) { 0046 return Import::Append; 0047 } else if(name == QLatin1String("merge")) { 0048 return Import::Merge; 0049 } 0050 return Import::Replace; 0051 } 0052 0053 QList<int> ApplicationInterface::selectedEntries() { 0054 QList<int> ids; 0055 foreach(Data::EntryPtr entry, Controller::self()->selectedEntries()) { 0056 ids << entry->id(); 0057 } 0058 return ids; 0059 } 0060 0061 QList<int> ApplicationInterface::filteredEntries() { 0062 QList<int> ids; 0063 foreach(Data::EntryPtr entry, Controller::self()->visibleEntries()) { 0064 ids << entry->id(); 0065 } 0066 return ids; 0067 } 0068 0069 void ApplicationInterface::openFile(const QString& file) { 0070 m_mainWindow->openFile(file); 0071 } 0072 0073 void ApplicationInterface::setFilter(const QString& text) { 0074 m_mainWindow->setFilter(text); 0075 } 0076 0077 bool ApplicationInterface::showEntry(int id) { 0078 return m_mainWindow->showEntry(id); 0079 } 0080 0081 bool ApplicationInterface::importFile(Tellico::Import::Format format, const QUrl& url, Tellico::Import::Action action) { 0082 return m_mainWindow->importFile(format, url, action); 0083 } 0084 0085 bool ApplicationInterface::exportCollection(Tellico::Export::Format format, const QUrl& url, bool filtered) { 0086 return m_mainWindow->exportCollection(format, url, filtered); 0087 } 0088 0089 CollectionInterface::CollectionInterface(QObject* parent_) : QObject(parent_) { 0090 QDBusConnection::sessionBus().registerObject(QStringLiteral("/Collections"), this, QDBusConnection::ExportScriptableSlots); 0091 } 0092 0093 int CollectionInterface::addEntry() { 0094 Data::CollPtr coll = Data::Document::self()->collection(); 0095 if(!coll) { 0096 return -1; 0097 } 0098 Data::EntryPtr entry(new Data::Entry(coll)); 0099 Kernel::self()->addEntries(Data::EntryList() << entry, false); 0100 return entry->id(); 0101 } 0102 0103 bool CollectionInterface::removeEntry(int id_) { 0104 Data::CollPtr coll = Data::Document::self()->collection(); 0105 if(!coll) { 0106 return false; 0107 } 0108 Data::EntryPtr entry = coll->entryById(id_); 0109 if(!entry) { 0110 return false; 0111 } 0112 Kernel::self()->removeEntries(Data::EntryList() << entry); 0113 return !coll->entryById(id_); 0114 } 0115 0116 QStringList CollectionInterface::allValues(const QString& fieldName_) { 0117 QStringList results; 0118 Data::CollPtr coll = Data::Document::self()->collection(); 0119 if(!coll) { 0120 return results; 0121 } 0122 Data::FieldPtr field = coll->fieldByName(fieldName_); 0123 if(!field) { 0124 field = coll->fieldByTitle(fieldName_); 0125 } 0126 if(!field) { 0127 return results; 0128 } 0129 Data::EntryList entries = Controller::self()->selectedEntries(); 0130 foreach(Data::EntryPtr entry, entries) { 0131 if(field->type() == Data::Field::Table) { 0132 results += FieldFormat::splitTable(entry->field(field)); 0133 } else { 0134 results += FieldFormat::splitValue(entry->field(field)); 0135 } 0136 } 0137 return results; 0138 } 0139 0140 QStringList CollectionInterface::entryValues(int id_, const QString& fieldName_) { 0141 QStringList results; 0142 Data::CollPtr coll = Data::Document::self()->collection(); 0143 if(!coll) { 0144 return results; 0145 } 0146 Data::FieldPtr field = coll->fieldByName(fieldName_); 0147 if(!field) { 0148 field = coll->fieldByTitle(fieldName_); 0149 } 0150 if(!field) { 0151 return results; 0152 } 0153 Data::EntryPtr entry = coll->entryById(id_); 0154 if(entry) { 0155 if(field->type() == Data::Field::Table) { 0156 results = FieldFormat::splitTable(entry->field(field)); 0157 } else { 0158 results = FieldFormat::splitValue(entry->field(field)); 0159 } 0160 } 0161 return results; 0162 } 0163 0164 QStringList CollectionInterface::selectedBibtexKeys() { 0165 Data::CollPtr coll = Data::Document::self()->collection(); 0166 if(!coll || coll->type() != Data::Collection::Bibtex) { 0167 return QStringList(); 0168 } 0169 return BibtexHandler::bibtexKeys(Controller::self()->selectedEntries()); 0170 } 0171 0172 QString CollectionInterface::entryBibtexKey(int id_) { 0173 Data::CollPtr coll = Data::Document::self()->collection(); 0174 if(!coll || coll->type() != Data::Collection::Bibtex) { 0175 return QString(); 0176 } 0177 Data::EntryPtr entry = coll->entryById(id_); 0178 if(!entry) { 0179 return QString(); 0180 } 0181 const QStringList keys = BibtexHandler::bibtexKeys(Data::EntryList() << entry); 0182 return keys.isEmpty() ? QString() : keys.first(); 0183 } 0184 0185 bool CollectionInterface::setEntryValue(int id_, const QString& fieldName_, const QString& value_) { 0186 Data::CollPtr coll = Data::Document::self()->collection(); 0187 if(!coll) { 0188 return false; 0189 } 0190 Data::EntryPtr entry = coll->entryById(id_); 0191 if(!entry) { 0192 return false; 0193 } 0194 Data::EntryPtr oldEntry(new Data::Entry(*entry)); 0195 if(!entry->setField(fieldName_, value_)) { 0196 return false; 0197 } 0198 Kernel::self()->modifyEntries(Data::EntryList() << oldEntry, Data::EntryList() << entry, 0199 QStringList() << fieldName_); 0200 return true; 0201 } 0202 0203 bool CollectionInterface::addEntryValue(int id_, const QString& fieldName_, const QString& value_) { 0204 Data::CollPtr coll = Data::Document::self()->collection(); 0205 if(!coll) { 0206 return false; 0207 } 0208 Data::EntryPtr entry = coll->entryById(id_); 0209 if(!entry) { 0210 return false; 0211 } 0212 Data::FieldPtr field = coll->fieldByName(fieldName_); 0213 if(!field) { 0214 return false; 0215 } 0216 0217 Data::EntryPtr oldEntry(new Data::Entry(*entry)); 0218 QStringList values; 0219 if(field->type() == Data::Field::Table) { 0220 values = FieldFormat::splitTable(entry->field(fieldName_)); 0221 } else { 0222 values = FieldFormat::splitValue(entry->field(fieldName_)); 0223 } 0224 QStringList newValues = values; 0225 newValues << value_; 0226 const QString del = field->type() == Data::Field::Table ? FieldFormat::rowDelimiterString() : FieldFormat::delimiterString(); 0227 if(!entry->setField(fieldName_, newValues.join(del))) { 0228 return false; 0229 } 0230 Kernel::self()->modifyEntries(Data::EntryList() << oldEntry, Data::EntryList() << entry, QStringList() << field->name()); 0231 return true; 0232 }