Merge commit 'upstream/0.3_20101226' into experimental
[debian/pino.git] / src / status_box.vala
1 using Gtk;
2
3 public class StatusBox : TextView {
4         
5         private Window parent;
6         private MenuIndicator indicator;
7         private Accounts accounts;
8         public VBox vbox;
9         private StatusChooseBar acc_box;
10         
11         private string reply_id = "";
12         
13         private int statuses_queue = 0;
14         
15         public StatusBox(Window parent, Accounts accounts) {
16                 this.parent = parent;
17                 this.indicator = ((MainWindow) parent).indicator;
18                 this.accounts = accounts;
19                 
20                 accounts.status_sent.connect((account, ok, msg) => {
21                         statuses_queue -= 1;
22                         
23                         if(statuses_queue < 1) {
24                                 indicator.hide_queue();
25                                 vbox.set_sensitive(true);
26                                 buffer.text = "";
27                         }
28                         
29                         debug("the end of status sending");
30                         //TODO
31                 });
32                 
33                 accounts.do_reply.connect(reply);
34                 
35                 wrap_mode = WrapMode.WORD_CHAR;
36                 
37                 vbox = new VBox(false, 0);
38                 
39                 Frame frame = new Frame(null);
40                 frame.add(this);
41                 
42                 acc_box = new StatusChooseBar(accounts);
43                 
44                 vbox.pack_start(frame, true, true, 0);
45                 vbox.pack_start(acc_box, false, false, 0);
46                 
47                 this.buffer.changed.connect(text_changed);
48                 this.key_press_event.connect(actions);
49         }
50         
51         private void text_changed() {
52                 int length = (int) buffer.text.len();
53                 acc_box.set_count(140 - length);
54         }
55         
56         private bool actions(Gdk.EventKey event) {
57                 switch(event.hardware_keycode) {
58                 case 36: //return key
59                         if(event.state == 1) { //shift + enter
60                                 buffer.insert_at_cursor("\n", (int)"\n".length);
61                                 return true;
62                         }
63                         if(buffer.text.length > 0) {
64                                 enter_pressed();
65                         } else { // if nothing to send
66                                 var message_dialog = new MessageDialog(parent,
67                                 Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL,
68                                 Gtk.MessageType.INFO, Gtk.ButtonsType.OK,
69                                 (_("Type something first")));
70                                 
71                                 message_dialog.run();
72                                 message_dialog.destroy();
73                         }
74                         return true;
75                 
76                 case 9: //esc key
77                         //clear();
78                         hide();
79                         break;
80                 }
81                 
82                 return false;
83         }
84         
85         private void reply(AAccount account, Status status) {
86                 debug("reply");
87                 acc_box.unselect_all();
88                 acc_box.select(account);
89                 
90                 reply_id = status.id;
91                 buffer.text = "@%s ".printf(status.user.name);
92                 
93                 parent.set_focus(this);
94         }
95         
96         private void enter_pressed() {
97                 debug("enter");
98                 
99                 if(settings.selected_for_posting.size < 1) {
100                         Gtk.MessageDialog dlg = new Gtk.MessageDialog(parent, Gtk.DialogFlags.MODAL,
101                                 Gtk.MessageType.INFO, Gtk.ButtonsType.OK,
102                                 _("You need to choose some accounts for sending statuses"));
103                         
104                         dlg.run();
105                         dlg.close();
106                         
107                         return;
108                 }
109                 
110                 vbox.set_sensitive(false);
111                 
112                 statuses_queue = settings.selected_for_posting.size;
113                 indicator.add_queue(_("Sending statuses: %d").printf(statuses_queue));
114                 
115                 foreach(string hash in settings.selected_for_posting) {
116                         AAccount? account = accounts.get_by_hash(hash);
117                         
118                         if(account == null)
119                                 continue;
120                         
121                         account.send_status(buffer.text, reply_id);
122                 }
123                 
124                 reply_id = "";
125         }
126 }