/* * Copyright (C) 2015 Aaron Seigo * Copyright (C) 2015 Christian Mollekopf * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "sinksh_utils.h" #include "common/store.h" #include "common/log.h" #include "utils.h" namespace SinkshUtils { bool isValidStoreType(const QString &type) { return Sink::ApplicationDomain::getTypeNames().contains(type.toLatin1()); } StoreBase &getStore(const QString &type) { using namespace Sink::ApplicationDomain; if (type == getTypeName()) { static Store store; return store; } else if (type == getTypeName()) { static Store store; return store; } else if (type == getTypeName()) { static Store store; return store; } else if (type == getTypeName()) { static Store store; return store; } else if (type == getTypeName()) { static Store store; return store; } else if (type == getTypeName()) { static Store store; return store; } SinkWarning_("", "") << "Trying to get a store that doesn't exist: " << type; Q_ASSERT(false); static DummyStore store; return store; } QList requestedProperties(const QString &type) { using namespace Sink::ApplicationDomain; if (type == getTypeName()) { return QList() << Folder::Name::name << Folder::Parent::name << Folder::SpecialPurpose::name; } else if (type == getTypeName()) { return QList() << Mail::Subject::name << Mail::Folder::name << Mail::Date::name; } else if (type == getTypeName()) { return QList() << Event::Summary::name; } else if (type == getTypeName()) { return QList() << SinkResource::ResourceType::name << SinkResource::Account::name << SinkResource::Capabilities::name; } else if (type == getTypeName()) { return QList() << SinkAccount::AccountType::name << SinkAccount::Name::name; } else if (type == getTypeName()) { return QList() << Identity::Name::name << Identity::Address::name << Identity::Account::name; } return QList(); } QSharedPointer loadModel(const QString &type, Sink::Query query) { query.requestedProperties = requestedProperties(type); auto model = getStore(type).loadModel(query); Q_ASSERT(model); return model; } QStringList resourceIds() { Sink::Query query; QStringList resources; for (const auto &r : getStore("resource").read(query)) { resources << r.identifier(); } return resources; } QStringList debugareaCompleter(const QStringList &, const QString &fragment, State &state) { return Utils::filteredCompletions(Sink::Log::debugAreas().toList(), fragment); } QStringList resourceCompleter(const QStringList &, const QString &fragment, State &state) { return Utils::filteredCompletions(resourceIds(), fragment); } static QStringList toStringList(const QByteArrayList &l) { QStringList list; for (const auto &s : l) { list << s; } return list; } QStringList resourceOrTypeCompleter(const QStringList &commands, const QString &fragment, State &state) { if (commands.count() == 1) { return Utils::filteredCompletions(toStringList(Sink::ApplicationDomain::getTypeNames()), fragment); } return Utils::filteredCompletions(resourceIds(), fragment); } QStringList typeCompleter(const QStringList &commands, const QString &fragment, State &state) { return Utils::filteredCompletions(toStringList(Sink::ApplicationDomain::getTypeNames()), fragment); } QMap keyValueMapFromArgs(const QStringList &args) { QMap map; for (int i = 0; i + 2 <= args.size(); i += 2) { map.insert(args.at(i), args.at(i + 1)); } return map; } bool isId(const QByteArray &value) { return value.startsWith("{"); } bool applyFilter(Sink::Query &query, const QStringList &args_) { if (args_.isEmpty()) { return false; } auto args = args_; auto type = args.takeFirst(); if ((type.isEmpty() || !SinkshUtils::isValidStoreType(type)) && type != "*") { qWarning() << "Unknown type: " << type; return false; } if (type != "*") { query.setType(type.toUtf8()); } if (!args.isEmpty()) { auto resource = args.takeFirst().toLatin1(); if (resource.contains('/')) { //The resource isn't an id but a path auto list = resource.split('/'); const auto resourceId = list.takeFirst(); query.resourceFilter(resourceId); if (type == Sink::ApplicationDomain::getTypeName() && !list.isEmpty()) { auto value = list.takeFirst(); if (isId(value)) { query.filter(value); } else { Sink::Query folderQuery; folderQuery.resourceFilter(resourceId); folderQuery.filter(value); folderQuery.filter(QVariant()); auto folders = Sink::Store::read(folderQuery); if (folders.size() == 1) { query.filter(folders.first()); } else { qWarning() << "Folder name did not match uniquely: " << folders.size(); for (const auto &f : folders) { qWarning() << f.getName(); } return false; } } } } else { query.resourceFilter(resource); } } return true; } bool applyFilter(Sink::Query &query, const SyntaxTree::Options &options) { return applyFilter(query, options.positionalArguments); } }