Imported Upstream version 0.2.7
[debian/pino.git] / src / pref_dialog.vala
1 /* pref_dialog.vala
2  *
3  * Copyright (C) 2009-2010  troorl
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author:
19  *      troorl <troorl@gmail.com>
20  */
21
22 using Gtk;
23 using Auth;
24
25 public class PrefDialog : Dialog {
26         
27         private Notebook tabs;
28         
29         private SpinButton updateInterval;
30         private SpinButton numberStatuses;
31         private ComboBox urlShorten;
32         private CheckButton enableSpell;
33         private CheckButton startMin;
34         private CheckButton showTray;
35         private CheckButton showTimelineNotify;
36         private CheckButton showMentionsNotify;
37         private CheckButton showDirectNotify;
38         
39         //private RadioButton showFullNotify;
40         private RadioButton showLowNotify;
41         
42         private ComboBox retweetStyle;
43         private Button deleteCache;
44         private CheckButton roundedAvatars;
45         private HScale opacityTweets;
46         private CheckButton rtlSupport;
47         private CheckButton fullNames;
48         private FontButton deFont;
49         private ColorButton freshColor;
50         private Entry login;
51         private Entry password;
52         private CheckButton savePass;
53         
54         public signal void delete_cache();
55         
56         public PrefDialog(Prefs prefs, Window parent, Accounts accounts) {
57                 this.modal = true;
58                 set_title(_("Preferences"));
59                 this.has_separator = false;
60                 
61                 tabs = new Notebook();
62                 
63                 //main page
64                 var main_box = new VBox(false, 10);
65                 
66                 //update interval
67                 var updateLabel = new Label(_("Update interval (in minutes)"));
68                 updateInterval = new SpinButton.with_range(1, 60, 1);
69                 
70                 //default number of statuses in lists
71                 var numberStatusesLabel = new Label(_("Default number of statuses"));
72                 numberStatuses = new SpinButton.with_range(5, 100, 1);
73                 
74                 //URL shortening service
75                 var urlShortenLabel = new Label(_("URL shortening service"));
76                 urlShorten = new ComboBox.text();
77                 urlShorten.append_text("goo.gl");
78                 urlShorten.append_text("is.gd");
79                 urlShorten.append_text("ur1.ca");
80                 
81                 //enabling spell checking
82                 enableSpell = new CheckButton.with_label(_("Enable spell checking"));
83                 
84                 //start in tray
85                 startMin = new CheckButton.with_label(_("Starting up in tray"));
86                 
87                 //show tray icon
88                 showTray = new CheckButton.with_label(_("Show tray icon"));
89                 
90                 //show notifications
91                 showTimelineNotify = new CheckButton.with_label(_("For timeline"));
92                 showMentionsNotify = new CheckButton.with_label(_("For mentions"));
93                 showDirectNotify = new CheckButton.with_label(_("For direct messages"));
94                 /*
95                 weak SList<weak Gtk.RadioButton> group_not  = null;
96                 showFullNotify = new RadioButton.with_label(group_not,
97                         _("Show notification for each status"));
98                 showLowNotify = new RadioButton.with_label(showFullNotify.get_group(),
99                         _("Show overall notification"));
100                 */
101                 //retweet style
102                 var reLabel = new Label(_("Retweets style"));
103                 retweetStyle = new ComboBox.text();
104                 retweetStyle.append_text("RT @username: message");
105                 retweetStyle.append_text("♺ @username: message");
106                 retweetStyle.append_text("message (via @username)");
107                 
108                 //delete cache
109                 deleteCache = new Button.with_label(_("Clear now"));
110                 var del_img = new Image.from_icon_name("gtk-clear", IconSize.BUTTON);
111                 deleteCache.set_image(del_img);
112                 deleteCache.clicked.connect(() => {
113                         delete_cache();
114                 });
115                 
116                 var table_int = new HigTable(_("General"));
117                 table_int.add_two_widgets(updateLabel, updateInterval);
118                 table_int.add_two_widgets(numberStatusesLabel, numberStatuses);
119                 table_int.add_two_widgets(urlShortenLabel, urlShorten);
120                 table_int.add_widget(enableSpell);
121                 
122                 var table_re = new HigTable(_("Retweets"));
123                 table_re.add_two_widgets(reLabel, retweetStyle);
124                 
125                 var table_cache = new HigTable(_("Cache"));
126                 table_cache.add_widget(deleteCache);
127                 
128                 main_box.pack_start(table_int, false, true, 10);
129                 main_box.pack_start(table_re, false, true, 10);
130                 main_box.pack_start(table_cache, false, true, 10);
131                 
132                 //desktop preferences
133                 var de_box = new VBox(false, 0);
134                 
135                 //tray
136                 var table_tr = new HigTable(_("Notification area"));
137                 table_tr.add_widget(startMin);
138                 table_tr.add_widget(showTray);
139                 
140                 var table_not = new HigTable(_("Notification"));
141                 table_not.add_widget(showTimelineNotify);
142                 table_not.add_widget(showMentionsNotify);
143                 table_not.add_widget(showDirectNotify);
144                 
145                 de_box.pack_start(table_tr, false, true, 10);
146                 de_box.pack_start(table_not, false, true, 10);
147                 
148                 //account page
149                 var ac_box = new VBox(false, 0);
150                 
151                 //accounts
152                 AccountWidget accountWidget = new AccountWidget(this, accounts);
153                 
154                 var table_auth = new HigTable(_("Authorization"));
155                 table_auth.add_widget_wide(accountWidget);
156                 
157                 ac_box.pack_start(table_auth, false, true, 10);
158                 
159                 if(prefs.is_new) {
160                         var start_box = new HBox(false, 0);
161                         var start_button = new Button.with_label(_("Create new account..."));
162                         
163                         start_button.clicked.connect(() => {
164                                 GLib.Pid pid;
165                                 GLib.Process.spawn_async(".", {"/usr/bin/xdg-open",
166                                         "http://twitter.com/signup"}, null,
167                                         GLib.SpawnFlags.STDOUT_TO_DEV_NULL, null, out pid);
168                         });
169                         
170                         start_box.pack_start(start_button, false, true, 10);
171                         ac_box.pack_start(start_box, false, true, 10);
172                 }
173                 
174                 //Appearance tab
175                 var app_box = new VBox(false, 0);
176                 
177                 var table_tweets = new HigTable(_("Statuses"));
178                 
179                 //rounded corners
180                 roundedAvatars = new CheckButton.with_label(_("Rounded corners"));
181                 
182                 //rtl support
183                 rtlSupport = new CheckButton.with_label(_("Right-to-left languages detection"));
184                 
185                 //full names or nicks in tweets
186                 fullNames = new CheckButton.with_label(_("Full names instead of nicknames"));
187                 
188                 //default font in statuses
189                 var deFontLabel = new Label(_("Default font"));
190                 deFont = new FontButton();
191                 deFont.set_use_size(true);
192                 deFont.set_show_style(false);
193                 
194                 //color of fresh statuses
195                 var freshColorLabel = new Label(_("Fresh statuses color"));
196                 freshColor = new ColorButton();
197                 freshColor.set_use_alpha(true);
198                 
199                 //opacity for tweets
200                 var opacityTweetsLabel = new Label(_("Opacity"));
201                 opacityTweets = new HScale.with_range(0, 100, 5);
202                 opacityTweets.set_size_request(150, -1);
203                 
204                 table_tweets.add_widget(roundedAvatars);
205                 table_tweets.add_widget(rtlSupport);
206                 table_tweets.add_widget(fullNames);
207                 table_tweets.add_two_widgets(deFontLabel, deFont);
208                 table_tweets.add_two_widgets(freshColorLabel, freshColor);
209                 table_tweets.add_two_widgets(opacityTweetsLabel, opacityTweets);
210                 
211                 app_box.pack_start(table_tweets, false, true, 10);
212                 
213                 tabs.append_page(main_box, new Label(_("Main")));
214                 tabs.append_page(de_box, new Label(_("Desktop")));
215                 tabs.append_page(ac_box, new Label(_("Account")));
216                 tabs.append_page(app_box, new Label(_("Appearance")));
217                 
218                 var hor_box = new HBox(false, 0);
219                 hor_box.pack_start(tabs, true, true, 10);
220                 
221                 this.vbox.pack_start(hor_box, true, true, 10);
222                 this.vbox.set_spacing(0);
223                 
224                 //action buttons
225                 add_button(STOCK_CLOSE, ResponseType.CLOSE);
226                 
227                 this.response.connect(response_acts);
228                 
229                 //setup preferences
230                 setup_prefs(prefs);
231                 
232                 //setup signals for prefs
233                 setup_prefs_signals(prefs);
234                 
235                 set_transient_for(parent);
236                 show_all();
237                 
238                 //if first start or don't want to remember the password
239                 if(accounts.is_new) {   
240                         tabs.set_current_page(2);
241                 }
242         }
243         
244         private void setup_urlshorten(Prefs prefs) {
245                 switch(prefs.urlShorten) {
246                         case "goo.gl":
247                                 urlShorten.set_active(0);
248                                 break;
249                         
250                         case "is.gd":
251                                 urlShorten.set_active(1);
252                                 break;
253                         
254                         case "ur1.ca":
255                                 urlShorten.set_active(2);
256                                 break;
257                         
258                         default:
259                                 urlShorten.set_active(0);
260                                 break;
261                 }
262         }
263         
264         private void setup_retweet(Prefs prefs) {
265                 switch(prefs.retweetStyle) {
266                         case ReTweet.Style.CLASSIC:
267                                 retweetStyle.set_active(0);
268                                 break;
269                         
270                         case ReTweet.Style.UNI:
271                                 retweetStyle.set_active(1);
272                                 break;
273                         
274                         case ReTweet.Style.VIA:
275                                 retweetStyle.set_active(2);
276                                 break;
277                 }
278         }
279         
280         private void setup_prefs(Prefs prefs) {
281                 updateInterval.value = prefs.updateInterval;
282                 numberStatuses.value = prefs.numberStatuses;
283                 setup_urlshorten(prefs);
284                 enableSpell.active = prefs.enableSpell;
285                 startMin.active = prefs.startMin;
286                 showTray.active = prefs.showTray;
287                 showTimelineNotify.active = prefs.showTimelineNotify;
288                 showMentionsNotify.active = prefs.showMentionsNotify;
289                 /*
290                 if(prefs.showFullNotify)
291                         showFullNotify.active = true;
292                 else
293                         showLowNotify.active = true;
294                 */
295                 showDirectNotify.active = prefs.showDirectNotify;
296                 setup_retweet(prefs);
297                 roundedAvatars.active = prefs.roundedAvatars;
298                 opacityTweets.set_value((int)(prefs.opacityTweets.to_double() * 100));
299                 rtlSupport.active = prefs.rtlSupport;
300                 fullNames.active = prefs.fullNames;
301                 deFont.set_font_name("%s %d".printf(prefs.deFontName, prefs.deFontSize));
302                 
303                 //colorFrsh setup
304                 uint16 alpha_fresh;
305                 var fresh_color = ColorUtils.rgba2color(prefs.freshColor, out alpha_fresh);
306                 //warning("%d", alpha_fresh);
307                 freshColor.set_color(fresh_color);
308                 freshColor.set_alpha(alpha_fresh);
309         }
310         
311         private void setup_prefs_signals(Prefs prefs) {
312                 updateInterval.value_changed.connect(() => {
313                         prefs.updateInterval = (int)updateInterval.value;
314                 });
315                 
316                 numberStatuses.value_changed.connect(() => {
317                         prefs.numberStatuses = (int)numberStatuses.value;
318                 });
319                 
320                 urlShorten.changed.connect(() => {
321                         switch(urlShorten.get_active()) {
322                                 case 0:
323                                         prefs.urlShorten = "goo.gl";
324                                         break;
325                                 
326                                 case 1:
327                                         prefs.urlShorten = "is.gd";
328                                         break;
329                                 
330                                 case 2:
331                                         prefs.urlShorten = "ur1.ca";
332                                         break;
333                         }
334                 });
335                 
336                 enableSpell.toggled.connect(() => {
337                         prefs.enableSpell = enableSpell.active;
338                 });
339                 
340                 startMin.toggled.connect(() => {
341                         prefs.startMin = startMin.active;
342                         
343                         if(prefs.startMin) //enabling tray
344                                 showTray.active = true;
345                 });
346                 
347                 showTray.toggled.connect(() => {
348                         prefs.showTray = showTray.active;
349                         
350                         if(!prefs.showTray) //disable starting in tray
351                                 startMin.active = false;
352                 });
353                 
354                 roundedAvatars.toggled.connect(() => {
355                         prefs.roundedAvatars = roundedAvatars.active;
356                 });
357                 
358                 opacityTweets.change_value.connect((scroll, new_value) => {
359                         if(new_value > 100)
360                                 new_value = 100;
361                         
362                         string str_val = (new_value / 100).to_string();
363                 
364                         if(str_val != "1" && str_val != "0") {
365                                 if(str_val.length < 4)
366                                         str_val = str_val.substring(1, 2);
367                                 else
368                                         str_val = str_val.substring(1, 3);
369                         }
370                 
371                         prefs.opacityTweets = str_val;
372                         return false;
373                 });
374                 
375                 rtlSupport.toggled.connect(() => {
376                         prefs.rtlSupport = rtlSupport.active;
377                 });
378                 
379                 fullNames.toggled.connect(() => {
380                         prefs.fullNames = fullNames.active;
381                 });
382                 
383                 deFont.font_set.connect(() => {
384                         //warning(deFont.get_font_name());
385                         prefs.deFont = deFont.get_font_name();
386                         //warning("font: %s, size: %d", prefs.deFontName, prefs.deFontSize);
387                 });
388                 
389                 freshColor.color_set.connect(() => {
390                         Gdk.Color fresh_color;
391                         freshColor.get_color(out fresh_color);
392                         double alpha = (double)freshColor.get_alpha() / 256.0 / 256.0;
393
394                         string result = ColorUtils.color2rgba(fresh_color, alpha);
395                         
396                         prefs.freshColor = result;
397                 });
398                 
399                 showTimelineNotify.toggled.connect(() => {
400                         prefs.showTimelineNotify = showTimelineNotify.active;
401                 });
402                 
403                 showMentionsNotify.toggled.connect(() => {
404                         prefs.showMentionsNotify = showMentionsNotify.active;
405                 });
406                 
407                 showDirectNotify.toggled.connect(() => {
408                         prefs.showDirectNotify = showDirectNotify.active;
409                 });
410                 /*
411                 showFullNotify.toggled.connect(() => {
412                         prefs.showFullNotify = showFullNotify.active;
413                 });
414                 */
415                 retweetStyle.changed.connect(() => {
416                         switch(retweetStyle.get_active()) {
417                                 case 0:
418                                         prefs.retweetStyle = ReTweet.Style.CLASSIC;
419                                         break;
420                                 
421                                 case 1:
422                                         prefs.retweetStyle = ReTweet.Style.UNI;
423                                         break;
424                                 
425                                 case 2:
426                                         prefs.retweetStyle = ReTweet.Style.VIA;
427                                         break;
428                         }
429                 });
430         }
431         
432         private void response_acts(int resp_id) {
433                 switch(resp_id) {
434                         case ResponseType.CLOSE:
435                                 close();
436                                 break;
437                 }
438         }       
439 }