/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
/* 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 cairo = imports.gi.cairo;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const _ = imports.gettext.gettext;
const Gettext = imports.gettext;

const SettingsData = imports.settings_data;
/* const Style = imports.style; */
const PreferencesUI = imports.preferences_ui;

const Application = new Lang.Class ({
  Name: "Application",
  Extends: Gtk.Application,
  
  _init: function () {
    this.settings = SettingsData.get_settings ();
    
    Gettext.bindtextdomain ("gpump", "./locale/"); //FIXME
    Gettext.textdomain ("gpump");
    GLib.set_prgname ("gpump");
    
    this.parent ({
      application_id: "org.gego.gpump",
      flags: Gio.ApplicationFlags.FLAGS_NONE,
      register_session: true,
    });
    GLib.set_application_name (_("GPump"));
    
  },
  
  vfunc_activate: function () {
    /* Guarantee that only one window is present at any time. */
    let w_list = this.get_windows ();
    if (w_list.length) {
      w_list[0].present ();
      return;
    }
    
    this._prepare_window ();
    this._prepare_app_menu ();
    this._prepare_header_bar ();
    
    /* Style.load_css (); */
    
    this.window.show_all ();
  },
  
  /**
   * This function is used to prepare the window, it also initializes and adds
   * the headerbar to it.
   */
  _prepare_window: function () {
    this.window = new Gtk.ApplicationWindow ({
      application: this,
      type: Gtk.WindowType.TOPLEVEL,
      title: GLib.get_application_name (),
      default_height: this.settings.get_setting ("ui.h").data,
      default_width: this.settings.get_setting ("ui.w").data,
      height_request: 500,
      width_request: 500,
      window_position: Gtk.WindowPosition.CENTER
    });
    
    this.window.set_titlebar ((this.headerbar = new Gtk.HeaderBar({
      title: GLib.get_application_name (),
      subtitle: _("A Pump.io client"),
      show_close_button: true
    })));
    
    if (!(this.settings.get_setting ("ui.x").data < 0 &&
        this.settings.get_setting ("ui.y").data < 0)){
      this.window.move (this.settings.get_setting ("ui.x").data,
                                       this.settings.get_setting ("ui.y").data);
    }
    
    var gtk_settings = Gtk.Settings.get_default ();
      if ( this.settings.get_setting ("main.use_dark").data ) {
        gtk_settings["gtk_application_prefer_dark_theme"] = true;
      } else {
        gtk_settings["gtk_application_prefer_dark_theme"] = false;
      }
    
    print ("derp!\n");
    
    this.window.add (( this.scroll_view = new Gtk.ScrolledWindow () ));
    this.scroll_view.add ((this.list_box = new Gtk.ListBox ()));
    
    this.window.connect ('delete_event',
                         Lang.bind (this.window,
                                    this.window.hide_on_delete));
    
    this.add_window (this.window);
  },
  
  /**
   * This function is used to create the application menu.
   */
  _prepare_app_menu: function () {
    let menu = new Gio.Menu ();
    let section = new Gio.Menu ();
    
    section.append (_("Preferences"), "app.preferences");
    menu.append_section (null, section);
    
    section = new Gio.Menu ();
    section.append (_("About"), "app.about");
    section.append (_("Quit"), "app.quit");
    menu.append_section (null, section);
    
    this.set_app_menu (menu);
    
    let about_action = new Gio.SimpleAction ({name: "about"});
    about_action.connect ('activate', Lang.bind (this, function () {
      let about_dialog = new Gtk.AboutDialog ({use_header_bar: true,
        transient_for: this.window,
        modal: true,
        version: "pre-alpha ~BZR~",
        program_name: GLib.get_application_name (),
        copyright: "Gustav \'Gego/XAREN\' Hartvigsson, 2014",
        license_type: Gtk.License.LGPL_3_0});
      
      about_dialog.authors =
                          ["Gustav Hartvigsson <gustav.hartvigsson@gmail.com>"];
      about_dialog.connect ("response", function () {
        about_dialog.destroy ();
      });
      about_dialog.run ();
    }));
    this.add_action (about_action);
    
    let quit_action = new Gio.SimpleAction ({name: "quit"});
    quit_action.connect ('activate', Lang.bind (this, function () {
      let allocation = this.window.get_allocation ();
      this.settings.set_setting ("ui.h", allocation.height);
      this.settings.set_setting ("ui.w", allocation.width);
      
      let win_pos = this.window.get_position ();
      this.settings.set_setting ("ui.x", win_pos[0]);
      this.settings.set_setting ("ui.y", win_pos[1]);
      this.settings.commit_to_file ();
      this.quit ();
    }));
    this.add_action (quit_action);
    
    let preferences_action = new Gio.SimpleAction ({name: "preferences"});
    preferences_action.connect ('activate', Lang.bind (this, function (){
      let pref_dialog = new PreferencesUI.PreferencesUI ();
      pref_dialog.set_transient_for (this.window);
      pref_dialog.run ();
    }));
    this.add_action (preferences_action);
  },
  
  /**
   * This function is used to prepare the hearderbar by adding buttons and
   * assigning callback functions to them.
   */
  _prepare_header_bar: function () {
    this.headerbar.pack_start ((this.new_post_btn = new Gtk.Button ({
      image: (new Gtk.Image ({icon_name: 'text-editor-symbolic'})),
      tooltip_text: _("Create new post")
    })));
    
    this.headerbar.pack_start ((this.refresh_btn = new Gtk.Button({
      image: (new Gtk.Image ({icon_name: 'emblem-synchronizing-symbolic'})),
      tooltip_text: _("Refresh the stream")
    })));
    
    this.headerbar.pack_end ((this.user_menu_btn = new Gtk.Button ({
      image: (new Gtk.Image ({icon_name: 'emblem-system-symbolic'})),
      tooltip_text: _("Switch and manage users")
    })));
  }
  
});