40ea0920e62e4b37339ea48d67215567931fb4dd
[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                 this.accounts.element_was_removed.connect(remove_account);
37         }
38         
39         public void set_count(int chars) {
40                 count.set_markup("<b>%d</b>".printf(chars));
41         }
42         
43         public void select(AAccount account) {
44                 foreach(Widget tb in this.get_children()) {
45                         if(tb.get_type() != typeof(ToggleToolButton))
46                                 continue;
47                         
48                         if(((ToggleToolButton) tb).label == account.get_hash()) {
49                                 ((ToggleToolButton) tb).set_active(true);
50                         }
51                 }
52                 
53                 update_config();
54         }
55         
56         public void unselect_all() {
57                 settings.selected_for_posting.clear();
58                                 
59                 foreach(Widget tb in this.get_children()) {
60                         if(tb.get_type() != typeof(ToggleToolButton))
61                                 continue;
62                         
63                         if(((ToggleToolButton) tb).active) {
64                                 ((ToggleToolButton) tb).set_active(false);
65                         }
66                 }
67         }
68         
69         private void update_config() {
70                 settings.selected_for_posting.clear();
71                                 
72                 foreach(Widget tb in this.get_children()) {
73                         if(tb.get_type() != typeof(ToggleToolButton))
74                                 continue;
75                         
76                         if(((ToggleToolButton) tb).active) {
77                                 settings.selected_for_posting.add(((ToolButton) tb).label);
78                         }
79                 }
80         }
81         
82         private void generate_acc() {
83                 foreach(AAccount account in this.accounts) {
84                         ToggleToolButton tb = new ToggleToolButton();
85                         tb.label = account.get_hash();
86                         tb.set_tooltip_text("%s (%s)".printf(account.s_name, account.id));
87                         
88                         if(settings.selected_for_posting.contains(tb.label))
89                                 tb.set_active(true);
90                         
91                         account.notify["userpic"].connect((s) => {
92                                 debug("userpic is loaded");
93                                 Image img = new Image.from_pixbuf(account.userpic.scale_simple(
94                                         24, 24, Gdk.InterpType.HYPER));
95                                 img.show();
96                                 tb.set_icon_widget(img);
97                         });
98                         
99                         tb.toggled.connect(() => {
100                                 update_config();
101                         });
102                         
103                         /*
104                         if(account.userpic != null) {
105                                 debug("userpic is ok");
106                                 Image img = new Image.from_pixbuf(account.userpic);
107                                 img.pixel_size = 48;
108                                 tb.set_icon_widget(img);
109                         }*/
110                         this.add(tb);
111                 }
112         }
113         
114         private void remove_account(string path, AAccount account) {
115                 if(path.contains(":")) //just stream, not account
116                         return;
117                 
118                 update_config();
119         }
120 }