File indexing completed on 2024-12-22 05:06:07
0001 /* 0002 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> 0003 * 0004 * This program is free software; you can redistribute it and/or modify 0005 * it under the terms of the GNU General Public License as published by 0006 * the Free Software Foundation; either version 2 of the License, or 0007 * (at your option) any later version. 0008 * 0009 * This program 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 0012 * GNU General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU General Public License 0015 * along with this program; if not, write to the 0016 * Free Software Foundation, Inc., 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 0018 */ 0019 0020 #include <QDebug> 0021 #include <QObject> // tr() 0022 #include <QTimer> 0023 0024 #include "common/resource.h" 0025 #include "common/storage.h" 0026 #include "common/resourcecontrol.h" 0027 #include "common/resourceconfig.h" 0028 #include "common/log.h" 0029 #include "common/storage.h" 0030 #include "common/definitions.h" 0031 #include "common/secretstore.h" 0032 0033 #include "sinksh_utils.h" 0034 #include "state.h" 0035 #include "syntaxtree.h" 0036 0037 namespace SinkSync 0038 { 0039 0040 bool sync(const QStringList &args, State &state) 0041 { 0042 auto options = SyntaxTree::parseOptions(args); 0043 if (options.options.value("password").isEmpty()) { 0044 state.printError(QObject::tr("Pass in a password with --password")); 0045 return false; 0046 } 0047 auto password = options.options.value("password").first(); 0048 0049 Sink::Query query; 0050 if (!options.positionalArguments.isEmpty() && !SinkshUtils::isValidStoreType(options.positionalArguments.first())) { 0051 //We have only specified a resource 0052 query.resourceFilter(SinkshUtils::parseUid(options.positionalArguments.first().toLatin1())); 0053 } else { 0054 //We have specified a full filter 0055 if (!SinkshUtils::applyFilter(query, options)) { 0056 state.printError(QObject::tr("Options: $type $resource/$folder/$subfolder --password $password")); 0057 return false; 0058 } 0059 } 0060 if (query.getResourceFilter().ids.isEmpty()) { 0061 state.printError(QObject::tr("Failed to find resource filter")); 0062 return false; 0063 } 0064 auto resourceId = query.getResourceFilter().ids.first(); 0065 Sink::SecretStore::instance().insert(resourceId, password); 0066 0067 Sink::Store::synchronize(query) 0068 .then(Sink::ResourceControl::flushMessageQueue(query.getResourceFilter().ids)) 0069 .then([state](const KAsync::Error &error) { 0070 int exitCode = 0; 0071 if (error) { 0072 state.printLine("Synchronization failed!"); 0073 exitCode = 1; 0074 } else { 0075 state.printLine("Synchronization complete!"); 0076 } 0077 state.commandFinished(exitCode); 0078 }).exec(); 0079 0080 return true; 0081 } 0082 0083 Syntax::List syntax() 0084 { 0085 Syntax sync("sync", QObject::tr("Synchronizes a resource."), &SinkSync::sync, Syntax::EventDriven); 0086 0087 sync.addPositionalArgument({"type", "The type of resource to synchronize"}); 0088 sync.addPositionalArgument({"resourceId", "The ID of the resource to synchronize"}); 0089 sync.addParameter("password", {"password", "The password of the resource", true}); 0090 0091 sync.completer = &SinkshUtils::resourceCompleter; 0092 0093 return Syntax::List() << sync; 0094 } 0095 0096 REGISTER_SYNTAX(SinkSync) 0097 0098 }