Skip to content

Commit

Permalink
Validate option values
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Mar 13, 2016
1 parent aa70dca commit 4c00429
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 131 deletions.
2 changes: 1 addition & 1 deletion src/app/clipboardserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void ClipboardServer::loadMonitorSettings()
QVariantMap settings;
settings["formats"] = m_itemFactory->formatsToSave();
#ifdef COPYQ_WS_X11
settings["check_selection"] = AppConfig().option("check_selection");
settings["check_selection"] = AppConfig().option<Config::check_selection>();
#endif

QByteArray settingsData;
Expand Down
7 changes: 7 additions & 0 deletions src/common/appconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@

#include "appconfig.h"

#include <QObject>
#include <QString>

QString defaultClipboardTabName()
{
return QObject::tr(
"&clipboard", "Default name of the tab that automatically stores new clipboard content");
}

QVariant AppConfig::option(const QString &name) const
{
return m_settings.value(name);
Expand Down
255 changes: 249 additions & 6 deletions src/common/appconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,248 @@

class QString;

#ifdef Q_OS_WIN
# define DEFAULT_EDITOR "notepad %1"
#elif defined(Q_OS_MAC)
# define DEFAULT_EDITOR "open -t %1"
#else
# define DEFAULT_EDITOR "gedit %1"
#endif

QString defaultClipboardTabName();

namespace Config {

template<typename ValueType>
struct Config {
typedef ValueType Value;
static Value defaultValue() { return Value(); }
static Value value(Value v) { return v; }
};

struct autostart : Config<bool> {
static QString name() { return "autostart"; }
};

struct maxitems : Config<int> {
static QString name() { return "maxitems"; }
static Value defaultValue() { return 200; }
static Value value(Value v) { return qBound(0, v, 10000); }
};

struct clipboard_tab : Config<QString> {
static QString name() { return "clipboard_tab"; }
static Value defaultValue() { return defaultClipboardTabName(); }
};

struct expire_tab : Config<bool> {
static QString name() { return "expire_tab"; }
};

struct editor : Config<QString> {
static QString name() { return "editor"; }
static Value defaultValue() { return DEFAULT_EDITOR; }
};

struct item_popup_interval : Config<int> {
static QString name() { return "item_popup_interval"; }
};

struct notification_position : Config<int> {
static QString name() { return "notification_position"; }
static Value defaultValue() { return 3; }
};

struct clipboard_notification_lines : Config<int> {
static QString name() { return "clipboard_notification_lines"; }
static Value value(Value v) { return qBound(0, v, 10000); }
};

struct notification_horizontal_offset : Config<int> {
static QString name() { return "notification_horizontal_offset"; }
static Value defaultValue() { return 10; }
};

struct notification_vertical_offset : Config<int> {
static QString name() { return "notification_vertical_offset"; }
static Value defaultValue() { return 10; }
};

struct notification_maximum_width : Config<int> {
static QString name() { return "notification_maximum_width"; }
static Value defaultValue() { return 300; }
};

struct notification_maximum_height : Config<int> {
static QString name() { return "notification_maximum_height"; }
static Value defaultValue() { return 100; }
};

struct edit_ctrl_return : Config<bool> {
static QString name() { return "edit_ctrl_return"; }
static Value defaultValue() { return true; }
};

struct move : Config<bool> {
static QString name() { return "move"; }
static Value defaultValue() { return true; }
};

struct check_clipboard : Config<bool> {
static QString name() { return "check_clipboard"; }
static Value defaultValue() { return true; }
};

struct confirm_exit : Config<bool> {
static QString name() { return "confirm_exit"; }
static Value defaultValue() { return true; }
};

struct vi : Config<bool> {
static QString name() { return "vi"; }
};

struct save_filter_history : Config<bool> {
static QString name() { return "save_filter_history"; }
};

struct always_on_top : Config<bool> {
static QString name() { return "always_on_top"; }
};

struct open_windows_on_current_screen : Config<bool> {
static QString name() { return "open_windows_on_current_screen"; }
static Value defaultValue() { return true; }
};

struct transparency_focused : Config<int> {
static QString name() { return "transparency_focused"; }
static Value value(Value v) { return qBound(0, v, 100); }
};

struct transparency : Config<int> {
static QString name() { return "transparency"; }
static Value value(Value v) { return qBound(0, v, 100); }
};

struct hide_tabs : Config<bool> {
static QString name() { return "hide_tabs"; }
};

struct hide_toolbar : Config<bool> {
static QString name() { return "hide_toolbar"; }
};

struct hide_toolbar_labels : Config<bool> {
static QString name() { return "hide_toolbar_labels"; }
static Value defaultValue() { return true; }
};

struct disable_tray : Config<bool> {
static QString name() { return "disable_tray"; }
};

struct hide_main_window : Config<bool> {
static QString name() { return "hide_main_window"; }
};

struct tab_tree : Config<bool> {
static QString name() { return "tab_tree"; }
};

struct show_tab_item_count : Config<bool> {
static QString name() { return "show_tab_item_count"; }
};

struct text_wrap : Config<bool> {
static QString name() { return "text_wrap"; }
static Value defaultValue() { return true; }
};

struct activate_closes : Config<bool> {
static QString name() { return "activate_closes"; }
static Value defaultValue() { return true; }
};

struct activate_focuses : Config<bool> {
static QString name() { return "activate_focuses"; }
};

struct activate_pastes : Config<bool> {
static QString name() { return "activate_pastes"; }
};


struct tray_items : Config<int> {
static QString name() { return "tray_items"; }
static Value defaultValue() { return 5; }
static Value value(Value v) { return qBound(0, v, 99); }
};

struct tray_item_paste : Config<bool> {
static QString name() { return "tray_item_paste"; }
static Value defaultValue() { return true; }
};

struct tray_commands : Config<bool> {
static QString name() { return "tray_commands"; }
static Value defaultValue() { return true; }
};

struct tray_tab_is_current : Config<bool> {
static QString name() { return "tray_tab_is_current"; }
static Value defaultValue() { return true; }
};

struct tray_images : Config<bool> {
static QString name() { return "tray_images"; }
static Value defaultValue() { return true; }
};

struct tray_tab : Config<QString> {
static QString name() { return "tray_tab"; }
};

struct command_history_size : Config<int> {
static QString name() { return "command_history_size"; }
static Value defaultValue() { return 100; }
};

struct check_selection : Config<bool> {
static QString name() { return "check_selection"; }
};

struct copy_clipboard : Config<bool> {
static QString name() { return "copy_clipboard"; }
};

struct copy_selection : Config<bool> {
static QString name() { return "copy_selection"; }
};

struct action_has_input : Config<bool> {
static QString name() { return "action_has_input"; }
};

struct action_has_output : Config<bool> {
static QString name() { return "action_has_output"; }
};

struct action_separator : Config<QString> {
static QString name() { return "action_separator"; }
static Value defaultValue() { return "\\n"; }
};

struct action_output_tab : Config<QString> {
static QString name() { return "action_output_tab"; }
};

struct tabs : Config<QStringList> {
static QString name() { return "tabs"; }
};

} // namespace Config

class AppConfig
{
public:
Expand All @@ -45,16 +287,17 @@ class AppConfig
return value.isValid() ? value.value<T>() : defaultValue;
}

bool isOptionOn(const QString &name) const
template <typename T>
typename T::Value option() const
{
return option(name).toBool();
typename T::Value value = option(T::name(), T::defaultValue());
return T::value(value);
}

template <typename T>
T optionInRange(const QString &name, T min, T max) const
bool isOptionOn(const QString &name, bool defaultValue = false) const
{
const T value = option(name).value<T>();
return qBound(min, value, max);
const QVariant value = option(name);
return value.isValid() ? value.toBool() : defaultValue;
}

void setOption(const QString &name, const QVariant &value);
Expand Down
16 changes: 8 additions & 8 deletions src/gui/clipboardbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ ClipboardBrowserShared::ClipboardBrowserShared(ItemFactory *itemFactory)
void ClipboardBrowserShared::loadFromConfiguration()
{
AppConfig appConfig;
editor = appConfig.option("editor").toString();
maxItems = appConfig.option("maxitems").toInt();
textWrap = appConfig.option("text_wrap").toBool();
viMode = appConfig.option("vi").toBool();
saveOnReturnKey = !appConfig.option("edit_ctrl_return").toBool();
moveItemOnReturnKey = appConfig.option("move").toBool();
minutesToExpire = appConfig.option("expire_tab").toInt();
editor = appConfig.option<Config::editor>();
maxItems = appConfig.option<Config::maxitems>();
textWrap = appConfig.option<Config::text_wrap>();
viMode = appConfig.option<Config::vi>();
saveOnReturnKey = !appConfig.option<Config::edit_ctrl_return>();
moveItemOnReturnKey = appConfig.option<Config::move>();
minutesToExpire = appConfig.option<Config::expire_tab>();
}

ClipboardBrowser::ClipboardBrowser(QWidget *parent, const ClipboardBrowserSharedPtr &sharedData)
Expand Down Expand Up @@ -435,7 +435,7 @@ void ClipboardBrowser::setEditorWidget(ItemEditorWidget *editor, bool changeClip
// Hide scrollbars while editing.
Qt::ScrollBarPolicy scrollbarPolicy = Qt::ScrollBarAlwaysOff;
if (!active) {
scrollbarPolicy = AppConfig(AppConfig::ThemeCategory).isOptionOn("show_scrollbars")
scrollbarPolicy = AppConfig(AppConfig::ThemeCategory).isOptionOn("show_scrollbars", true)
? Qt::ScrollBarAsNeeded : Qt::ScrollBarAlwaysOff;
}
setVerticalScrollBarPolicy(scrollbarPolicy);
Expand Down
Loading

0 comments on commit 4c00429

Please sign in to comment.