OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2013 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include <iostream> |
| 19 #include <sstream> |
| 20 |
| 21 #include "PrefsCommand.h" |
| 22 |
| 23 PrefsCommand::PrefsCommand(AdblockPlus::FilterEngine& filterEngine) |
| 24 : Command("prefs"), filterEngine(filterEngine) |
| 25 { |
| 26 } |
| 27 |
| 28 void PrefsCommand::operator()(const std::string& arguments) |
| 29 { |
| 30 std::istringstream argumentStream(arguments); |
| 31 std::string action; |
| 32 argumentStream >> action; |
| 33 if (!action.size()) |
| 34 { |
| 35 ShowUsage(); |
| 36 return; |
| 37 } |
| 38 |
| 39 if (action == "show") |
| 40 { |
| 41 std::string pref; |
| 42 argumentStream >> pref; |
| 43 |
| 44 AdblockPlus::JsValuePtr value = filterEngine.GetPref(pref); |
| 45 if (value->IsUndefined()) |
| 46 std::cout << "No such preference" << std::endl; |
| 47 else |
| 48 { |
| 49 if (value->IsString()) |
| 50 std::cout << "(string) "; |
| 51 else if (value->IsNumber()) |
| 52 std::cout << "(number) "; |
| 53 else if (value->IsBool()) |
| 54 std::cout << "(bool) "; |
| 55 else |
| 56 std::cout << "(unknown type) "; |
| 57 std::cout << value->AsString() << std::endl; |
| 58 } |
| 59 } |
| 60 else if (action == "set") |
| 61 { |
| 62 std::string pref; |
| 63 argumentStream >> pref; |
| 64 |
| 65 AdblockPlus::JsValuePtr current = filterEngine.GetPref(pref); |
| 66 if (current->IsUndefined()) |
| 67 std::cout << "No such preference" << std::endl; |
| 68 else if (current->IsString()) |
| 69 { |
| 70 std::string value; |
| 71 std::getline(argumentStream, value); |
| 72 filterEngine.SetPref(pref, filterEngine.GetJsEngine()->NewValue(value)); |
| 73 } |
| 74 else if (current->IsNumber()) |
| 75 { |
| 76 int64_t value; |
| 77 argumentStream >> value; |
| 78 filterEngine.SetPref(pref, filterEngine.GetJsEngine()->NewValue(value)); |
| 79 } |
| 80 else if (current->IsBool()) |
| 81 { |
| 82 bool value; |
| 83 argumentStream >> value; |
| 84 filterEngine.SetPref(pref, filterEngine.GetJsEngine()->NewValue(value)); |
| 85 } |
| 86 else |
| 87 std::cout << "Cannot set a preference of unknown type" << std::endl; |
| 88 } |
| 89 else |
| 90 throw NoSuchCommandError(name + " " + action); |
| 91 } |
| 92 |
| 93 std::string PrefsCommand::GetDescription() const |
| 94 { |
| 95 return "Get and set preferences"; |
| 96 } |
| 97 |
| 98 std::string PrefsCommand::GetUsage() const |
| 99 { |
| 100 return name + " [show PREF|set PREF VALUE]"; |
| 101 } |
OLD | NEW |