/gpump/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/gpump/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* This file is part of GPump, a Pump.io client.
 *
 * GPump (THE SOFTWARE) is made available under the terms and conditions of the
 * GNU Lesser General Public Licence 3.0. A copy of the licence can be read
 * in the file lgpl-3.0.txt in the root of this project.
 */

const Gtk = imports.gi.Gtk;
const _ = imports.gettext.gettext;
const Lang = imports.lang;

const SettingsData = imports.settings_data;

/** @class
 * PreferencesUI.
 */
const PreferencesUI = Lang.Class ({
  Name: "PrefrencesUI",
  Extends: Gtk.Dialog,
  
  _init: function (params) {
    this.parent ({
      use_header_bar: true,
      modal: true,
      title: _("GPump preferences"),
      width_request: 550,
      height_request: 300
    });
    
    if (params != undefined) {
      for (let k in params) {
        this[k] = params[k];
      }
    }
    
    this._prepare_header_bar ();
    this._prepare_list_box ();
    
    this.set_property ('resizable', false);
    
    this.show_all ();
  },
  
  _prepare_header_bar: function () {
    
    this.headerbar = this.get_header_bar ();
    this.headerbar.set_show_close_button (false);
    this.headerbar.pack_end ((this.close_btn = new Gtk.Button ({
      label: _("close")
    })));
    
    /* Install a custom style */
    let special_btn_style_ctx = this.close_btn.get_style_context ();
    special_btn_style_ctx.add_class ("suggested-action");
    
    this.close_btn.connect ('clicked', Lang.bind (this, function () {
      SettingsData.get_settings ().commit_to_file ();
      this.destroy ();
    }));
    
  },
  
  _prepare_list_box: function () {
    /*this.get_content_area ().pack_start ()),
                                         true, true, 25);*/
    this.get_content_area ().set_center_widget (
      (this.layout = new Gtk.Box ({
        orientation: Gtk.Orientation.VERTICAL,
        spacing: 8,
        border_width: 8,
        margin: 50
      }))
    );
    
    
    this.layout.pack_start (
      new Gtk.Frame ({child:
        (this.list_box = new Gtk.ListBox ({
          width_request: 400,
          selection_mode: Gtk.SelectionMode.NONE,
          expand: false
        })),
        expand: false
      }),
      true,
      true,
      null
    );
    
    let settings_object = SettingsData.get_settings ();
    
    /*************************** Use dark setting *****************************/
    
    this.use_dark_hbox = new Gtk.Box ({orientation: Gtk.Orientation.HORIZONTAL,
                                       spacing: 5});
    
    this.list_box.add (this.use_dark_hbox);
    
    this.use_dark_hbox.pack_start ((new Gtk.Label ({
      label: _("Use dark theme")
    })),false, false, 3);
    
    this.use_dark_hbox.pack_end ((this.use_dark_reset = new Gtk.Button ({
      image: (new Gtk.Image ({icon_name: "edit-clear"}))
    })),false, false, 3);
    
    this.use_dark_hbox.pack_end ((this.use_dark_switch = new Gtk.Switch ()),
                                 false, false, 3);
    
    this.use_dark_switch.set_active (
      settings_object.get_setting("main.use_dark").data
    );
    
    this.use_dark_switch.connect("notify::active",
                                                  Lang.bind (this, function () {
      let settings = Gtk.Settings.get_default ();
      let app_settings = SettingsData.get_settings ();
      if ( this.use_dark_switch.get_active () ) {
        settings["gtk_application_prefer_dark_theme"] = true;
        app_settings.set_setting ("main.use_dark", true);
      } else {
        settings["gtk_application_prefer_dark_theme"] = false;
        app_settings.set_setting ("main.use_dark", false);
      }
    }));
    
    this.use_dark_reset.connect ("clicked", Lang.bind (this,
                                 this._reset_use_dark_theme));
    
    /*********************** Quit on close ************************************/
    this.quit_on_close_hbox = new Gtk.Box ({
      orientation: Gtk.Orientation.HORIZONTAL,
      spacing: 5
    });
    
    this.list_box.add (this.quit_on_close_hbox);
    
    this.quit_on_close_hbox.pack_start ((new Gtk.Label ({
      label: _("Quit on close")
    })), false, false, 3);
    
    this.quit_on_close_hbox.pack_end ((this.quit_on_close_reset =
                                       new Gtk.Button ({
      image: (new Gtk.Image ({icon_name: "edit-clear"}))
    })), false, false, 3);
    
    this.quit_on_close_hbox.pack_end ((this.quit_on_close_switch =
                                       new Gtk.Switch), false, false, 3);
    
    if (settings_object.get_setting ("main.quit_on_close").data !=
                                       SettingsData.QUIT_ON_CLOSE.UNKNOWN) {
      this.quit_on_close_switch.set_active (Boolean (
        settings_object.get_setting ("main.quit_on_close")));
      print ("Herro!")
    } else {
      this.quit_on_close_switch.set_active (false);
    }
    
    this.quit_on_close_switch.connect ("notify::active",
                                       Lang.bind (this, function () {
      let v = settings_object.get_setting ("main.quit_on_close").data;
      
      switch (v) {
        case (SettingsData.QUIT_ON_CLOSE.YES):
        case (SettingsData.QUIT_ON_CLOSE.NO):
          if (this.quit_on_close_switch.get_active ()) {
            settings_object.set_setting ("main.quit_on_close", SettingsData.QUIT_ON_CLOSE.YES);
          } else {
            settings_object.set_setting ("main.quit_on_close", SettingsData.QUIT_ON_CLOSE.NO);
          }
          break;
          break;
        case (SettingsData.QUIT_ON_CLOSE.UNKNOWN):
          let popover = new Gtk.Popover ({relative_to: this.quit_on_close_switch, modal: true});
          let popover_layout1 = new Gtk.Box ({parent: popover, orientation: Gtk.Orientation.VERTICAL});
          popover_layout1.pack_start ((new Gtk.Label ({
            label: _("This has not been set yet, if you like to set it to a " +
            "value, click \"yes\" or \"no\", dismiss this to not set a value"),
            max_width_chars: 25,
            wrap: true
          })),false,false,0);
          let popover_layout2 = new Gtk.Box ({orientation: Gtk.Orientation.HORIZONTAL});
          popover_layout2.get_style_context().add_class ("linked");
          let btn_yes = new Gtk.Button ({label: _("Yes")});
          let btn_no = new Gtk.Button ({label: _("No")});
          popover_layout2.pack_end (btn_yes, true, true, 0);
          popover_layout2.pack_start (btn_no, true, true, 0);
          popover_layout1.pack_end (popover_layout2, false, false, 0);
          btn_no.get_style_context ().add_class("suggested-action");
          
          popover.connect_after ("hide", Lang.bind (popover, function () {
            popover.destroy ();
          }));
          
          btn_no.connect ("clicked", Lang.bind ((popover, this), function () {
            SettingsData.get_settings ().set_setting ("main.quit_on_close",
            SettingsData.QUIT_ON_CLOSE.NO);
            this.quit_on_close_switch.set_active (false);
            popover.hide ();
          }));
          
          btn_yes.connect ("clicked", Lang.bind ((popover, this), function () {
            SettingsData.get_settings ().set_setting ("main.quit_on_close",
            SettingsData.QUIT_ON_CLOSE.YES);
            this.quit_on_close_switch.set_active (true);
            popover.hide ();
          }));
          
          popover.show_all ();
          break;
        default:
          print ("WOW! Something went wrong!");
          print (JSON.stringify(v).toString ());
      }
    }));
    
    
    /*************************** Privacy settings *****************************/
    this.list_box.add ((new Gtk.Label ({label: _("Privacy settings")})));
    
    /************************ show full name setting **************************/
    
    this.show_full_name_hbox = new Gtk.Box ({
      orientation: Gtk.Orientation.HORIZONTAL,
      spacing: 5
    });
    
    this.list_box.add (this.show_full_name_hbox);
    
    this.show_full_name_hbox.pack_start ((new Gtk.Label ({
      label: _("Show full name")
    })),false, false, 3);
    
    this.show_full_name_hbox.pack_end ((this.show_full_name_reset =
      new Gtk.Button ({
        image: (new Gtk.Image ({icon_name: "edit-clear"}))
      })), false, false, 3);
    
    this.show_full_name_hbox.pack_end (
      (this.show_full_name_switch = new Gtk.Switch ()), false, false, 3);
    
    this.show_full_name_switch.set_active (
      settings_object.get_setting ("main.privacy.show_full_name").data
    );
    
    this.show_full_name_switch.connect ("notify::active",
                                        Lang.bind (this, function () {
      let app_settings = SettingsData.get_settings ();
      if (this.show_full_name_switch.get_active ()) {
        app_settings.set_setting ("main.privacy.show_full_name", true);
      } else {
        app_settings.set_setting ("main.privacy.show_full_name", false);
      }
    }));
    
    this.show_full_name_reset.connect ("clicked", Lang.bind (this,
                                                  this._reset_show_full_name));
    
    /*********************** only show avatar setting **************************/
    
    this.only_show_avatar_hbox = new Gtk.Box ({orientation: Gtk.Orientation.HORIZONTAL,
                                       spacing: 5});
    
    this.list_box.add (this.only_show_avatar_hbox);
    
    this.only_show_avatar_hbox.pack_start ((new Gtk.Label ({
      label: _("Only show avatar")
    })), false, false, 3);
    
    this.only_show_avatar_hbox.pack_end ((this.only_show_avatar_reset = 
                                          new Gtk.Button ({
      image: (new Gtk.Image ({icon_name: "edit-clear"}))
    })), false, false, 3);
    
    this.only_show_avatar_hbox.pack_end (
                             (this.only_show_avatar_switch = new Gtk.Switch ()),
                                 false, false, 3);
    this.only_show_avatar_switch.set_active (
      settings_object.get_setting ("main.privacy.only_show_avatar").data
    );
    
    this.only_show_avatar_switch.connect ("notify::active",
                                          Lang.bind (this, function () {
      let app_settings = SettingsData.get_settings ();
      if (this.only_show_avatar_switch.get_active ()) {
        app_settings.set_setting ("main.privacy.only_show_avatar", true);
      } else {
        app_settings.set_setting ("main.privacy.only_show_avatar", false);
      }
    }));
    
    /* Reset all settings */
    this.layout.pack_end (
      (this.reset_all_btn = new Gtk.Button ({
        label: _("Reset all settings")
      })),
      false,
      false,
      null
    );
    
    this.reset_all_btn.connect ("clicked", Lang.bind(this, this._reset_all));
  },
  
  _reset_all: function () {
    this._reset_use_dark_theme ();
    this._reset_show_full_name ();
    this._reset_only_show_avatar ();
    this._reset_quit_on_close
  },
  
  _reset_use_dark_theme: function () {
    /* If the switch is active */
    if ((this.use_dark_switch.get_active ())) {
      this.use_dark_switch.set_active (false);
    }
  },
  
  _reset_show_full_name: function () {
    if (!(this.show_full_name_switch.get_active ())) {
      this.show_full_name_switch.set_active (true);
    }
  },
  
  _reset_only_show_avatar: function () {
    if ((this.only_show_avatar_switch.get_active ())) {
      this.only_show_avatar_switch.set_active (false);
    }
  },
  
  _reset_quit_on_close: function () {
    if ((this.quit_on_close_switch.get_active())) {
      this.quit_on_close_switch.set_active (false);
    }
  }
  
});