Merge branch 'upstream' into experimental
[debian/pino.git] / src / status_choose_bar.vala
1 using Gtk;
2
3 public class StatusChooseBar : Toolbar {
4         
5         private Accounts accounts;
6         private ToolButton open_img;
7         private Label count;
8         
9         public StatusChooseBar(Accounts accounts) {
10                 this.accounts = accounts;
11                 
12                 open_img = new ToolButton(null, _("Upload image"));
13                 open_img.set_icon_name("gtk-open");
14                 this.add(open_img);
15                 
16                 SeparatorToolItem separator1 = new SeparatorToolItem();
17                 separator1.set_draw(false);
18                 this.add(separator1);
19                 
20                 count = new Label("");
21                 count.modify_font(Pango.FontDescription.from_string("sans 18"));
22                 ToolItem to = new ToolItem();
23                 to.add(count);
24                 add(to);
25                 
26                 SeparatorToolItem separator2 = new SeparatorToolItem();
27                 separator2.set_draw(false);
28                 separator2.set_expand(true);
29                 this.add(separator2);
30                 
31                 generate_acc();
32                 
33                 set_icon_size(IconSize.SMALL_TOOLBAR);
34                 toolbar_style = ToolbarStyle.ICONS;
35                 
36                 accounts.insert_new_account.connect((acc) => {
37                         add_new_account(acc);
38                         update_config();
39                 });
40                 
41                 accounts.element_was_removed.connect((path, acc) => {
42                         if(path.contains(":")) //just stream, not account
43                         return;
44                 
45                         foreach(Widget tb in this.get_children()) {
46                                 if(tb.get_type() != typeof(ToggleToolButton))
47                                         continue;
48                                 
49                                 if(((ToggleToolButton) tb).label == acc.get_hash()) {
50                                         this.remove(tb);
51                                 }
52                         }
53                         
54                         update_config();
55                 });
56         }
57         
58         public void set_count(int chars) {
59                 count.set_markup("<b>%d</b>".printf(chars));
60         }
61         
62         public void select(AAccount account) {
63                 foreach(Widget tb in this.get_children()) {
64                         if(tb.get_type() != typeof(ToggleToolButton))
65                                 continue;
66                         
67                         if(((ToggleToolButton) tb).label == account.get_hash()) {
68                                 ((ToggleToolButton) tb).set_active(true);
69                         }
70                 }
71                 
72                 update_config();
73         }
74         
75         public void unselect_all() {
76                 settings.selected_for_posting.clear();
77                                 
78                 foreach(Widget tb in this.get_children()) {
79                         if(tb.get_type() != typeof(ToggleToolButton))
80                                 continue;
81                         
82                         if(((ToggleToolButton) tb).active) {
83                                 ((ToggleToolButton) tb).set_active(false);
84                         }
85                 }
86         }
87         
88         private void update_config() {
89                 settings.selected_for_posting.clear();
90                                 
91                 foreach(Widget tb in this.get_children()) {
92                         if(tb.get_type() != typeof(ToggleToolButton))
93                                 continue;
94                         
95                         if(((ToggleToolButton) tb).active) {
96                                 settings.selected_for_posting.add(((ToolButton) tb).label);
97                         }
98                 }
99         }
100         
101         private void add_new_account(AAccount account) {
102                 debug(account.s_name);
103                 ToggleToolButton tb = new ToggleToolButton();
104                 tb.label = account.get_hash();
105                 tb.set_tooltip_text("%s (%s)".printf(account.s_name, account.id));
106                 
107                 if(settings.selected_for_posting.contains(tb.label))
108                         tb.set_active(true);
109                 
110                 account.notify["userpic"].connect((s) => {
111                         debug("userpic is loaded");
112                         Image img = new Image.from_pixbuf(account.userpic.scale_simple(
113                                 24, 24, Gdk.InterpType.HYPER));
114                         img.show();
115                         tb.set_icon_widget(img);
116                 });
117                 
118                 tb.toggled.connect(() => {
119                         update_config();
120                 });
121                 
122                 /*
123                 if(account.userpic != null) {
124                         debug("userpic is ok");
125                         Image img = new Image.from_pixbuf(account.userpic);
126                         img.pixel_size = 48;
127                         tb.set_icon_widget(img);
128                 }*/
129                 this.add(tb);
130                 tb.show();
131         }
132         
133         private void generate_acc() {
134                 foreach(AAccount account in this.accounts) {
135                         add_new_account(account);
136                 }
137         }
138 }