/gpump/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/gpump/trunk
33 by Gustav Hartvigsson
* Main window no on par with the C version
1
/* This file is part of GPump, a Pump.io client.
2
 *
3
 * GPump (THE SOFTWARE) is made available under the terms and conditions of the
4
 * GNU Lesser General Public Licence 3.0. A copy of the licence can be read
5
 * in the file lgpl-3.0.txt in the root of this project.
6
 */
7
52.1.1 by Gustav Hartvigsson
Button still not worknig, saving for prosparitny
8
9
const Gettext = imports.gettext;
33 by Gustav Hartvigsson
* Main window no on par with the C version
10
const Gio = imports.gi.Gio;
11
const GLib = imports.gi.GLib;
46 by Gustav Hartvigsson
* Now saves the Height, Width, X and Y of the window on quit.
12
const GObject = imports.gi.GObject;
33 by Gustav Hartvigsson
* Main window no on par with the C version
13
const Gtk = imports.gi.Gtk;
52.1.1 by Gustav Hartvigsson
Button still not worknig, saving for prosparitny
14
const _ = imports.gettext.gettext;
33 by Gustav Hartvigsson
* Main window no on par with the C version
15
const Lang = imports.lang;
16
17
const SettingsData = imports.settings_data;
49 by Gustav Hartvigsson
* Changes a thing in the about dialoug.
18
/* const Style = imports.style; */
44 by Gustav Hartvigsson
* UI for preferences done, it is still not functional.
19
const PreferencesUI = imports.preferences_ui;
52.1.2 by Gustav Hartvigsson
* Fixed a few style errors...
20
const UserMenu = imports.user_menu;
52.1.10 by Gustav Hartvigsson
* made the UserButton work in tests.
21
const UserButton = imports.user_button;
33 by Gustav Hartvigsson
* Main window no on par with the C version
22
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
23
/** @class
24
 * This class is the main application class, it creates the main wiev
25
 * and basically is what runs the program.
26
 */
33 by Gustav Hartvigsson
* Main window no on par with the C version
27
const Application = new Lang.Class ({
28
  Name: "Application",
35 by Gustav Hartvigsson
* Changed to extending Gtk.Application instead....
29
  Extends: Gtk.Application,
33 by Gustav Hartvigsson
* Main window no on par with the C version
30
  
31
  _init: function () {
46 by Gustav Hartvigsson
* Now saves the Height, Width, X and Y of the window on quit.
32
    this.settings = SettingsData.get_settings ();
33
    
52 by Gustav Hartvigsson
* Added translation files (messeges.po and sv.po)
34
    Gettext.bindtextdomain ("gpump", "./locale/"); //FIXME
35
    Gettext.textdomain ("gpump");
36
    GLib.set_prgname ("gpump");
37
    
35 by Gustav Hartvigsson
* Changed to extending Gtk.Application instead....
38
    this.parent ({
33 by Gustav Hartvigsson
* Main window no on par with the C version
39
      application_id: "org.gego.gpump",
40
      flags: Gio.ApplicationFlags.FLAGS_NONE,
46 by Gustav Hartvigsson
* Now saves the Height, Width, X and Y of the window on quit.
41
      register_session: true,
33 by Gustav Hartvigsson
* Main window no on par with the C version
42
    });
51 by Gustav Hartvigsson
* woops
43
    GLib.set_application_name (_("GPump"));
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
44
    
33 by Gustav Hartvigsson
* Main window no on par with the C version
45
  },
46
  
36 by Gustav Hartvigsson
* changed from this.connect(...) to vfunc_ .
47
  vfunc_activate: function () {
33 by Gustav Hartvigsson
* Main window no on par with the C version
48
    /* Guarantee that only one window is present at any time. */
35 by Gustav Hartvigsson
* Changed to extending Gtk.Application instead....
49
    let w_list = this.get_windows ();
33 by Gustav Hartvigsson
* Main window no on par with the C version
50
    if (w_list.length) {
51
      w_list[0].present ();
55 by Gustav Hartvigsson
* Code cleanup
52
      w_list[0].move (this.settings.get_setting ("ui.x").data,
53
                             this.settings.get_setting ("ui.y").data);
33 by Gustav Hartvigsson
* Main window no on par with the C version
54
      return;
55
    }
56
    
43 by Gustav Hartvigsson
* made the functions look more alike...
57
    this._prepare_window ();
58
    this._prepare_app_menu ();
59
    this._prepare_header_bar ();
33 by Gustav Hartvigsson
* Main window no on par with the C version
60
    
49 by Gustav Hartvigsson
* Changes a thing in the about dialoug.
61
    /* Style.load_css (); */
46 by Gustav Hartvigsson
* Now saves the Height, Width, X and Y of the window on quit.
62
    
33 by Gustav Hartvigsson
* Main window no on par with the C version
63
    this.window.show_all ();
64
  },
65
  
66
  /**
67
   * This function is used to prepare the window, it also initializes and adds
68
   * the headerbar to it.
69
   */
43 by Gustav Hartvigsson
* made the functions look more alike...
70
  _prepare_window: function () {
53 by Gustav Hartvigsson
* Made the window behave when it is shown
71
    
38 by Gustav Hartvigsson
* Can now load settings from file.
72
    this.window = new Gtk.ApplicationWindow ({
73
      application: this,
33 by Gustav Hartvigsson
* Main window no on par with the C version
74
      type: Gtk.WindowType.TOPLEVEL,
38 by Gustav Hartvigsson
* Can now load settings from file.
75
      title: GLib.get_application_name (),
53 by Gustav Hartvigsson
* Made the window behave when it is shown
76
      default_height: 500,
77
      default_width: 500,
33 by Gustav Hartvigsson
* Main window no on par with the C version
78
      height_request: 500,
38 by Gustav Hartvigsson
* Can now load settings from file.
79
      width_request: 500,
80
      window_position: Gtk.WindowPosition.CENTER
33 by Gustav Hartvigsson
* Main window no on par with the C version
81
    });
82
    
83
    this.window.set_titlebar ((this.headerbar = new Gtk.HeaderBar({
38 by Gustav Hartvigsson
* Can now load settings from file.
84
      title: GLib.get_application_name (),
52 by Gustav Hartvigsson
* Added translation files (messeges.po and sv.po)
85
      subtitle: _("A Pump.io client"),
33 by Gustav Hartvigsson
* Main window no on par with the C version
86
      show_close_button: true
87
    })));
88
    
46 by Gustav Hartvigsson
* Now saves the Height, Width, X and Y of the window on quit.
89
    if (!(this.settings.get_setting ("ui.x").data < 0 &&
90
        this.settings.get_setting ("ui.y").data < 0)){
50 by Gustav Hartvigsson
* bound the GtkSwitch for "use dark theme"
91
      this.window.move (this.settings.get_setting ("ui.x").data,
92
                                       this.settings.get_setting ("ui.y").data);
46 by Gustav Hartvigsson
* Now saves the Height, Width, X and Y of the window on quit.
93
    }
94
    
50 by Gustav Hartvigsson
* bound the GtkSwitch for "use dark theme"
95
    var gtk_settings = Gtk.Settings.get_default ();
96
      if ( this.settings.get_setting ("main.use_dark").data ) {
97
        gtk_settings["gtk_application_prefer_dark_theme"] = true;
98
      } else {
99
        gtk_settings["gtk_application_prefer_dark_theme"] = false;
100
      }
101
    
38 by Gustav Hartvigsson
* Can now load settings from file.
102
    print ("derp!\n");
103
    
52.1.6 by Gustav Hartvigsson
* added StackSwitcher for the main and meanwhile wievs.
104
    
105
    
106
    this.window.add ((this.layout = new Gtk.Box ({
107
      orientation: Gtk.Orientation.VERTICAL
108
    })));
109
    
110
    /* * Building the stack switched views */
111
    /* switcher */
112
    /* Hack to centre the damn switcher */
113
    this.layout.pack_start ((this.switcher_box = new Gtk.Box ({
114
      orientation: Gtk.Orientation.HORIZONTAL
115
    })), false, false, 0);
116
    
117
    this.switcher_box.set_center_widget (
118
      (this.stack_switcher = new Gtk.StackSwitcher ())
119
    );
120
    
121
    /* the stack */
122
    this.layout.pack_end ((this.stack = new Gtk.Stack ({
123
      homogeneous: true,
124
      transition_duration: 250,
125
      transition_type: Gtk.StackTransitionType.CROSSFADE
126
    })), true, true, 0);
127
    
128
    /* main view */
129
    this.stack.add_titled ((new Gtk.ScrolledWindow ({
130
      child: (this.main_list_box = new Gtk.ListBox()),
131
      margin: 1
132
    })), "main", _("Main"));
133
    
134
    
135
    /* 'mean while' view */
136
    this.stack.add_titled ((new Gtk.ScrolledWindow ({
137
      child: (this.mw_list_box = new Gtk.ListBox()),
138
      margin: 1
139
    })), "meanwhile", _("Meanwhile"));
140
    
141
    /* * Hook up the stack and the stack switcher */
142
    this.stack_switcher.set_stack (this.stack);
143
    
144
    /* * Hide the window so, so we can show it later */
33 by Gustav Hartvigsson
* Main window no on par with the C version
145
    this.window.connect ('delete_event',
53 by Gustav Hartvigsson
* Made the window behave when it is shown
146
                         Lang.bind (this, function () {
55 by Gustav Hartvigsson
* Code cleanup
147
      print ("balls to it!");
53 by Gustav Hartvigsson
* Made the window behave when it is shown
148
      this._save_settings ();
55 by Gustav Hartvigsson
* Code cleanup
149
      this.window.hide ();
150
      /* To Prevent the widow from rely being destroyed? */
151
      return true;
53 by Gustav Hartvigsson
* Made the window behave when it is shown
152
    }));
33 by Gustav Hartvigsson
* Main window no on par with the C version
153
    
35 by Gustav Hartvigsson
* Changed to extending Gtk.Application instead....
154
    this.add_window (this.window);
33 by Gustav Hartvigsson
* Main window no on par with the C version
155
  },
156
  
157
  /**
158
   * This function is used to create the application menu.
159
   */
43 by Gustav Hartvigsson
* made the functions look more alike...
160
  _prepare_app_menu: function () {
33 by Gustav Hartvigsson
* Main window no on par with the C version
161
    let menu = new Gio.Menu ();
162
    let section = new Gio.Menu ();
163
    
52 by Gustav Hartvigsson
* Added translation files (messeges.po and sv.po)
164
    section.append (_("Preferences"), "app.preferences");
33 by Gustav Hartvigsson
* Main window no on par with the C version
165
    menu.append_section (null, section);
166
    
167
    section = new Gio.Menu ();
34 by Gustav Hartvigsson
* added GetText stuffs...
168
    section.append (_("About"), "app.about");
169
    section.append (_("Quit"), "app.quit");
33 by Gustav Hartvigsson
* Main window no on par with the C version
170
    menu.append_section (null, section);
171
    
35 by Gustav Hartvigsson
* Changed to extending Gtk.Application instead....
172
    this.set_app_menu (menu);
33 by Gustav Hartvigsson
* Main window no on par with the C version
173
    
47 by Gustav Hartvigsson
* Fixed the preferences dialoug. For some reasons
174
    let about_action = new Gio.SimpleAction ({name: "about"});
49 by Gustav Hartvigsson
* Changes a thing in the about dialoug.
175
    about_action.connect ('activate', Lang.bind (this, function () {
47 by Gustav Hartvigsson
* Fixed the preferences dialoug. For some reasons
176
      let about_dialog = new Gtk.AboutDialog ({use_header_bar: true,
50 by Gustav Hartvigsson
* bound the GtkSwitch for "use dark theme"
177
        transient_for: this.window,
178
        modal: true,
179
        version: "pre-alpha ~BZR~",
180
        program_name: GLib.get_application_name (),
181
        copyright: "Gustav \'Gego/XAREN\' Hartvigsson, 2014",
182
        license_type: Gtk.License.LGPL_3_0});
48 by Gustav Hartvigsson
* added moar random information to the about dialoug.
183
      
50 by Gustav Hartvigsson
* bound the GtkSwitch for "use dark theme"
184
      about_dialog.authors =
185
                          ["Gustav Hartvigsson <gustav.hartvigsson@gmail.com>"];
47 by Gustav Hartvigsson
* Fixed the preferences dialoug. For some reasons
186
      about_dialog.connect ("response", function () {
187
        about_dialog.destroy ();
188
      });
189
      about_dialog.run ();
50 by Gustav Hartvigsson
* bound the GtkSwitch for "use dark theme"
190
    }));
47 by Gustav Hartvigsson
* Fixed the preferences dialoug. For some reasons
191
    this.add_action (about_action);
192
    
33 by Gustav Hartvigsson
* Main window no on par with the C version
193
    let quit_action = new Gio.SimpleAction ({name: "quit"});
194
    quit_action.connect ('activate', Lang.bind (this, function () {
53 by Gustav Hartvigsson
* Made the window behave when it is shown
195
      this._save_settings ();
35 by Gustav Hartvigsson
* Changed to extending Gtk.Application instead....
196
      this.quit ();
33 by Gustav Hartvigsson
* Main window no on par with the C version
197
    }));
35 by Gustav Hartvigsson
* Changed to extending Gtk.Application instead....
198
    this.add_action (quit_action);
33 by Gustav Hartvigsson
* Main window no on par with the C version
199
    
44 by Gustav Hartvigsson
* UI for preferences done, it is still not functional.
200
    let preferences_action = new Gio.SimpleAction ({name: "preferences"});
201
    preferences_action.connect ('activate', Lang.bind (this, function (){
55 by Gustav Hartvigsson
* Code cleanup
202
      let pref_dialog = new PreferencesUI.PreferencesUI ({
203
        transient_for: this.window
204
      });
44 by Gustav Hartvigsson
* UI for preferences done, it is still not functional.
205
      pref_dialog.run ();
206
    }));
50 by Gustav Hartvigsson
* bound the GtkSwitch for "use dark theme"
207
    this.add_action (preferences_action);
33 by Gustav Hartvigsson
* Main window no on par with the C version
208
  },
209
  
210
  /**
211
   * This function is used to prepare the hearderbar by adding buttons and
212
   * assigning callback functions to them.
213
   */
43 by Gustav Hartvigsson
* made the functions look more alike...
214
  _prepare_header_bar: function () {
52.1.12 by Gustav Hartvigsson
* Made a few style changes
215
    
57 by Gustav Hartvigsson
* Made sure that the icons are square
216
    this.headerbar.pack_start ((this.new_post_btn = new Gtk.Button ({
33 by Gustav Hartvigsson
* Main window no on par with the C version
217
      image: (new Gtk.Image ({icon_name: 'text-editor-symbolic'})),
52.1.12 by Gustav Hartvigsson
* Made a few style changes
218
      tooltip_text: _("Create new post"),
57 by Gustav Hartvigsson
* Made sure that the icons are square
219
    })));
220
    
221
   this.new_post_btn.get_style_context ().add_class ("image-button");
222
    
223
    this.headerbar.pack_start ((this.refresh_btn = new Gtk.Button({
33 by Gustav Hartvigsson
* Main window no on par with the C version
224
      image: (new Gtk.Image ({icon_name: 'emblem-synchronizing-symbolic'})),
52.1.12 by Gustav Hartvigsson
* Made a few style changes
225
      tooltip_text: _("Refresh the stream"),
57 by Gustav Hartvigsson
* Made sure that the icons are square
226
    })));
227
    
228
    this.refresh_btn.get_style_context ().add_class ("image-button");
33 by Gustav Hartvigsson
* Main window no on par with the C version
229
    
52.1.11 by Gustav Hartvigsson
* switched the UserButton to be a GtkMenuButton instead of a GtkButton
230
    this.headerbar.pack_end ((this.user_menu_btn = new UserButton.UserButton ({
52.1.7 by Gustav Hartvigsson
* changed the 'user icon' temporarely, until I get the
231
      image: (new Gtk.Image ({icon_name: 'mail-send-symbolic'})),
52.1.2 by Gustav Hartvigsson
* Fixed a few style errors...
232
      tooltip_text: _("Switch and manage users"),
233
      popover: (this.user_menu = new UserMenu.UserMenu ()),
33 by Gustav Hartvigsson
* Main window no on par with the C version
234
    })));
52.1.2 by Gustav Hartvigsson
* Fixed a few style errors...
235
    
53 by Gustav Hartvigsson
* Made the window behave when it is shown
236
  },
237
  
238
  _save_settings: function () {
239
    let allocation = this.window.get_allocation ();
240
    this.settings.set_setting ("ui.h", allocation.height);
241
    this.settings.set_setting ("ui.w", allocation.width);
242
    
243
    let win_pos = this.window.get_position ();
244
    this.settings.set_setting ("ui.x", win_pos[0]);
245
    this.settings.set_setting ("ui.y", win_pos[1]);
246
    this.settings.commit_to_file ();
52.1.7 by Gustav Hartvigsson
* changed the 'user icon' temporarely, until I get the
247
  }
33 by Gustav Hartvigsson
* Main window no on par with the C version
248
  
249
});