Skip to content

Commit

Permalink
Use GLib.Settings instead of deprecated Granite.Services.Settings (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryonakano authored Apr 10, 2020
1 parent a89f705 commit 351b93e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 80 deletions.
7 changes: 7 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@
namespace Lockbox {
public class Application : Gtk.Application {
public static string app_cmd_name;
public static GLib.Settings saved_state;
public static GLib.Settings app_settings;

construct {
flags |= ApplicationFlags.HANDLES_OPEN;

application_id = Constants.PROJECT_NAME;
}

static construct {
saved_state = new GLib.Settings (Constants.PROJECT_NAME + ".saved-state");
app_settings = new GLib.Settings (Constants.PROJECT_NAME + ".settings");
}

public Application () {
Intl.setlocale (LocaleCategory.ALL, "");
string langpack_dir = Path.build_filename (Constants.INSTALL_PREFIX, "share", "locale");
Expand Down
6 changes: 2 additions & 4 deletions src/Dialogs/PreferencesDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ namespace Lockbox.Dialogs {
}

construct {
var settings = Services.Settings.get_default ();

var grid = new Gtk.Grid ();
grid.column_spacing = 12;
grid.row_spacing = 6;
Expand All @@ -57,7 +55,7 @@ namespace Lockbox.Dialogs {
var clear_clipboard_timeout_label = new SettingsLabel (_("Timeout (secs):"));
clear_clipboard_timeout = new Gtk.Entry ();
clear_clipboard_timeout.input_purpose = Gtk.InputPurpose.DIGITS;
clear_clipboard_timeout.text = settings.clear_clipboard_timeout.to_string ();
clear_clipboard_timeout.text = Application.app_settings.get_int ("clear-clipboard-timeout").to_string ();
clear_clipboard_timeout.activates_default = true;
grid.attach (clear_clipboard_timeout_label, 0, 2, 1, 1);
grid.attach (clear_clipboard_timeout, 1, 2, 1, 1);
Expand Down Expand Up @@ -94,7 +92,7 @@ namespace Lockbox.Dialogs {
public SettingsSwitch (string setting) {
halign = Gtk.Align.START;
valign = Gtk.Align.CENTER;
Services.Settings.get_default ().schema.bind (setting, this, "active", SettingsBindFlags.DEFAULT);
Application.app_settings.bind (setting, this, "active", SettingsBindFlags.DEFAULT);
}
}
}
Expand Down
49 changes: 24 additions & 25 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,21 @@ namespace Lockbox {
app.set_accels_for_action (ACTION_PREFIX + action, action_accelerators[action].to_array ());
}

/* Load State and Settings */
var saved_state = Services.SavedState.get_default ();
set_default_size (saved_state.window_width, saved_state.window_height);
if (saved_state.window_x == -1 || saved_state.window_y == -1) {
set_default_size (Application.saved_state.get_int ("window-width"), Application.saved_state.get_int ("window-height"));

var window_x = Application.saved_state.get_int ("window-x");
var window_y = Application.saved_state.get_int ("window-y");
if (window_x == -1 || window_y == -1) {
window_position = Gtk.WindowPosition.CENTER;
} else {
move (saved_state.window_x, saved_state.window_y);
move (window_x, window_y);
}

if (saved_state.maximized) {
if (Application.saved_state.get_boolean ("maximized")) {
this.maximize ();
}

Gtk.Settings.get_default ().gtk_application_prefer_dark_theme = Services.Settings.get_default ().dark_theme;
Gtk.Settings.get_default ().gtk_application_prefer_dark_theme = Application.app_settings.get_boolean ("dark-theme");

/* Init Clipboard */
clipboard = Gtk.Clipboard.get_for_display (get_display (), Gdk.SELECTION_CLIPBOARD);
Expand All @@ -123,7 +124,7 @@ namespace Lockbox {
collection_list.selection_mode = Gtk.SelectionMode.NONE;
collection_list.set_filter_func (CollectionFilterFunc);
collection_list.activate_on_single_click = false;
set_sort_func (Services.Settings.get_default ().sort_by);
set_sort_func ((Services.Sort) Application.app_settings.get_enum ("sort-by"));
scroll_window.add (collection_list);
layout_stack.add_named (scroll_window, "collection");

Expand All @@ -148,12 +149,11 @@ namespace Lockbox {
});

headerbar.sort.connect((sort_by) => {
var settings = Services.Settings.get_default ();
if (sort_by != settings.sort_by) {
settings.sort_by = sort_by;
settings.sort_desc = true;
if (sort_by != Application.app_settings.get_enum ("sort-by")) {
Application.app_settings.set_enum ("sort-by", sort_by);
Application.app_settings.set_boolean ("sort-desc", true);
} else {
settings.sort_desc = !settings.sort_desc;
Application.app_settings.set_boolean ("sort-desc", !Application.app_settings.get_boolean ("sort-desc"));
}
set_sort_func (sort_by);
});
Expand Down Expand Up @@ -247,18 +247,17 @@ namespace Lockbox {
}

private void update_saved_state () {
var saved_state = Services.SavedState.get_default ();
int window_width;
int window_height;
int window_x;
int window_y;
get_size (out window_width, out window_height);
get_position (out window_x, out window_y);
saved_state.window_width = window_width;
saved_state.window_height = window_height;
saved_state.window_x = window_x;
saved_state.window_y = window_y;
saved_state.maximized = is_maximized;
Application.saved_state.set_int ("window-width", window_width);
Application.saved_state.set_int ("window-height", window_height);
Application.saved_state.set_int ("window-x", window_x);
Application.saved_state.set_int ("window-y", window_y);
Application.saved_state.set_boolean ("maximized", is_maximized);
}

private void populate_list (List<Secret.Item> items) {
Expand Down Expand Up @@ -307,7 +306,7 @@ namespace Lockbox {
private void copy_username (Secret.Item item) {
var username = item.attributes.get ("username");
clipboard.set_text (username, -1);
if (Services.Settings.get_default ().clear_clipboard) {
if (Application.app_settings.get_boolean ("clear-clipboard")) {
reset_clipboard_timer ();
}
}
Expand All @@ -316,7 +315,7 @@ namespace Lockbox {
item.load_secret.begin (new Cancellable (), (obj, res) => {
var password = item.get_secret ().get_text ();
clipboard.set_text (password, -1);
if (Services.Settings.get_default ().clear_clipboard) {
if (Application.app_settings.get_boolean ("clear-clipboard")) {
reset_clipboard_timer ();
}
});
Expand All @@ -339,7 +338,7 @@ namespace Lockbox {
}

private bool clear_clipboard_timed_out () {
if (Services.Settings.get_default ().clear_clipboard) {
if (Application.app_settings.get_boolean ("clear-clipboard")) {
GLib.Source.remove (clipboard_timer_id);
clipboard_timer_id = 0;
clipboard.clear ();
Expand All @@ -352,7 +351,7 @@ namespace Lockbox {
GLib.Source.remove (clipboard_timer_id);
clipboard_timer_id = 0;
}
clipboard_timer_id = GLib.Timeout.add_seconds (Services.Settings.get_default ().clear_clipboard_timeout,
clipboard_timer_id = GLib.Timeout.add_seconds (Application.app_settings.get_int ("clear-clipboard-timeout"),
clear_clipboard_timed_out);
}

Expand Down Expand Up @@ -390,15 +389,15 @@ namespace Lockbox {
private int CollectionSortNameFunc (Gtk.ListBoxRow row1, Gtk.ListBoxRow row2) {
var collection_row1 = row1 as Widgets.CollectionListRow;
var collection_row2 = row2 as Widgets.CollectionListRow;
var desc = Services.Settings.get_default ().sort_desc ? 1 : -1;
var desc = Application.app_settings.get_boolean ("sort-desc") ? 1 : -1;

return collection_row1.item.label.ascii_casecmp (collection_row2.item.label) * desc;
}

private int CollectionSortDateFunc (Gtk.ListBoxRow row1, Gtk.ListBoxRow row2) {
var collection_row1 = row1 as Widgets.CollectionListRow;
var collection_row2 = row2 as Widgets.CollectionListRow;
var desc = Services.Settings.get_default ().sort_desc ? 1 : -1;
var desc = Application.app_settings.get_boolean ("sort-desc") ? 1 : -1;

if (collection_row1.item.created < collection_row2.item.created) {
return -1 * desc;
Expand Down
51 changes: 0 additions & 51 deletions src/Services/Settings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,4 @@ namespace Lockbox.Services {
NAME,
CREATED
}

public class SavedState : Granite.Services.Settings {
public int window_width { get; set; }
public int window_height { get; set; }
public int window_x { get; set; }
public int window_y { get; set; }
public bool maximized { get; set; }

public SavedState () {
base (Constants.PROJECT_NAME + ".saved-state");
}

private static SavedState saved_state;
public static unowned SavedState get_default () {
if (saved_state == null) {
saved_state = new SavedState ();
}
return saved_state;
}
}

public class Settings : Granite.Services.Settings {
public bool clear_clipboard { get; set; }
public int clear_clipboard_timeout { get; set; }
public bool dark_theme { get; set; }
public Sort sort_by { get; set; }
public bool sort_desc { get; set; }

public Settings () {
base (Constants.PROJECT_NAME + ".settings");
}

public override void verify (string key) {
switch (key) {
case "clear-clipboard":
Granite.Services.Logger.notification ("Changed clear clipboard setting");
break;
case "dark-mode":
Granite.Services.Logger.notification ("Changed dark mode setting");
break;
}
}

public static Settings settings;
public static unowned Settings get_default () {
if (settings == null) {
settings = new Settings ();
}
return settings;
}
}
}

0 comments on commit 351b93e

Please sign in to comment.