-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.php
326 lines (289 loc) · 13.4 KB
/
Main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* Client.php - \Callicore\Twitter\Client main window class
*
* This is released under the MIT, see license.txt for details
*
* @author Elizabeth Smith <[email protected]>
* @copyright Elizabeth Smith (c)2009
* @link http://callicore.net
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @version $Id: Main.php 25 2009-04-30 23:58:52Z auroraeosrose $
* @since Php 5.3.0
* @package callicore
* @subpackage lib
* @filesource
*/
/**
* Namespace for application
*/
namespace Callicore\Twitter;
use Callicore\Lib\Application; // for connecting the app quit to window close
use Callicore\Lib\Window; // for remember state functionality
use \Gtk; // we use constants from here
use \Gdk; // we use constants from here
use \GtkStatusbar; // need this for the window
use \GtkToolbar; // also need this
use \GtkToolButton; // more gtk classes
use \GtkEntry;
use \GtkLabel;
use GtkListStore;
use GtkVBox;
use GtkScrolledWindow;
use GtkTreeView;
use GtkTreeViewColumn;
use GtkCellRendererPixbuf;
use GtkCellRendererText;
use GdkPixbuf;
use Gobject;
class Main extends Window {
protected $statusicon;
protected $twitter;
protected $treeview;
protected $statusbar;
protected $temp;
protected $pic_queue = array();
protected $pic_cached = array();
protected $public_timeline_timeout;
protected $load_images_timeout;
public function __construct() {
parent::__construct();
$t = Application::getInstance()->translate;
$this->set_icon_from_file(\Callicore\Lib\DIR. '/icons/logo.png');
$this->set_size_request(300, 500);
$this->set_title($t->_('Callicore Twitter'));
$this->connect_simple('destroy', array(Application::getInstance(), 'quit'));
// realize this to grab the gdkwindow, turn maximize off
$this->realize();
$this->window->set_decorations(Gdk::DECOR_BORDER | Gdk::DECOR_RESIZEH | Gdk::DECOR_TITLE | Gdk::DECOR_MENU | Gdk::DECOR_MINIMIZE);
// Status Icon is only used if GtkStatusIcon exists
if (class_exists('\GtkStatusIcon', false)) { // we use the absolute namespace name here, remember use autoloads
$this->statusicon = new StatusIcon();
$this->statusicon->connect('activate', array($this->statusicon,
'activate_window'), $this);
$this->set_skip_taskbar_hint(true);
$this->connect('window-state-event', array($this, 'minimize_to_tray'));
}
$this->temp = sys_get_temp_dir() . 'php-gtk-twitter-api-cache\\';
if (!file_exists($this->temp)) {
mkdir($this->temp, null, true);
}
$this->twitter = new Api;
$this->statusbar = new GtkStatusBar();
// Create a toolbar with login button
$tb = new GtkToolbar();
$tb->set_show_arrow(false);
$this->loginbutton = GtkToolButton::new_from_stock(Gtk::STOCK_JUMP_TO);
$this->loginbutton->set_label('Login');
$this->loginbutton->connect_simple('clicked', array($this, 'login'));
$tb->insert($this->loginbutton, -1);
// logout button, hide it
$this->logoutbutton = GtkToolButton::new_from_stock(Gtk::STOCK_CLOSE);
$this->logoutbutton->set_label('Logout');
$this->logoutbutton->connect_simple('clicked', array($this, 'logout'));
$tb->insert($this->logoutbutton, -1);
$this->logoutbutton->set_sensitive(false);
// Create an update area
$this->updateentry = new GtkEntry();
$this->updateentry->set_max_length(140);
$this->updateentry->set_sensitive(false);
$this->updateentry->connect('activate', array($this, 'send_update'));
$this->entrystatus = new GtkLabel();
// User image pixbuf, user image string, user name, user id, text, favorited, created_at, id
$store = new GtkListStore(GdkPixbuf::gtype, Gobject::TYPE_STRING, Gobject::TYPE_STRING,
Gobject::TYPE_LONG, GObject::TYPE_STRING, GObject::TYPE_BOOLEAN, GObject::TYPE_STRING,
Gobject::TYPE_LONG);
$store->set_sort_column_id(7, Gtk::SORT_DESCENDING);
$list = $this->twitter->get_public_timeline();
$this->statusbar->push(1, 'last updated ' . date('Y-m-d H:i') . ' - ' . count($list) . ' new tweets');
// stuff the store
foreach($list as $object) {
$store->append(array(null, $object->user->profile_image_url, $object->user->name,
$object->user->id, $object->text, $object->favorited, $object->created_at,
$object->id));
}
$this->public_timeline_timeout = Gtk::timeout_add(61000, array($this, 'update_public_timeline')); // every 60 seconds
// stuff vbox
$vbox = new GtkVBox();
$this->add($vbox);
$vbox->pack_start($tb, false, false);
$scrolled = new GtkScrolledWindow();
$scrolled->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
$vbox->pack_start($scrolled);
$this->treeview = new GtkTreeView($store);
$scrolled->add($this->treeview);
$this->treeview->set_property('headers-visible', false);
$this->treeview->set_rules_hint(true);
// This is temporary, for testing add the gtknotebook
$this->notebook = new Notebook;
$vbox->pack_start($this->notebook, false, false);
$vbox->pack_start(new GtkLabel('What are you doing?'), false, false);
$vbox->pack_start($this->updateentry, false, false);
$vbox->pack_start($this->entrystatus, false, false);
$vbox->pack_start($this->statusbar, false, false);
$picture_renderer = new GtkCellRendererPixbuf();
$picture_column = new GtkTreeViewColumn('Picture', $picture_renderer, 'pixbuf', 0);
$picture_column->set_cell_data_func($picture_renderer, array($this, 'show_user'));
$this->treeview->append_column($picture_column);
$message_renderer = new GtkCellRendererText();
$message_renderer->set_property('wrap-mode', Gtk::WRAP_WORD);
$message_renderer->set_property('wrap-width', 200);
$message_renderer->set_property('width', 10);
$message_column = new GtkTreeViewColumn('Message', $message_renderer);
$message_column->set_cell_data_func($message_renderer, array($this, 'message_markup'));
$this->treeview->append_column($message_column);
$this->treeview->set_resize_mode(Gtk::RESIZE_IMMEDIATE);
}
public function show_user($column, $cell, $store, $position) {
$pic = $store->get_value($position, 1);
$name = $this->temp . md5($pic);
if (isset($this->pic_queue[$name])) {
return;
} elseif (isset($this->pic_cached[$name])) {
$store = $this->treeview->get_model();
if (is_null($store->get_value($position, 0))) {
$pixbuf = GdkPixbuf::new_from_file($name . '.jpg');
$store->set($position, 0, $pixbuf);
$cell->set_property('pixbuf', $pixbuf);
}
return;
}
$this->pic_queue[$name] = array('name' => $name, 'url' => $pic,
'pos' => $position, 'cell' => $cell);
if (empty($this->load_images_timeout)) {
$this->load_images_timeout = Gtk::timeout_add(500, array($this, 'pic_queue'));
}
}
public function pic_queue() {
$pic = array_shift($this->pic_queue);
if (empty($pic)) {
$this->load_images_timeout = null;
return true;
}
if (!file_exists($pic['name'])) {
file_put_contents($pic['name'] . '.jpg', file_get_contents($pic['url']));
$this->pic_cached[$pic['name']] = $pic['url'];
}
return true; // keep the timeout going
}
public function message_markup($column, $cell, $store, $position) {
$user = utf8_decode($store->get_value($position, 2));
$message = utf8_decode($store->get_value($position, 4));
$time = $this->distance($store->get_value($position, 6));
$message = htmlspecialchars_decode($message, ENT_QUOTES);
$message = str_replace(array('@' . $user, ' ', '&'), array('<span foreground="#FF6633">@' . $user . '</span>', ' ', '&'), $message);
$cell->set_property('markup', "<b>$user</b>:\n$message\n<small>$time</small>");
}
protected function distance($from) {
$minutes = round(abs(time() - strtotime($from)) / 60);
switch(true) {
case ($minutes == 0):
return 'less than 1 minute ago';
case ($minutes < 1):
return '1 minute ago';
case ($minutes <= 55):
return $minutes . ' minutes ago';
case ($minutes <= 65):
return 'about 1 hour ago';
case ($minutes <= 1439):
return 'about ' . round((float) $minutes / 60.0) . ' hours';
case ($minutes <= 2879):
return '1 day ago';
default:
return 'about ' . round((float) $minutes / 1440) . ' days ago';
}
}
public function minimize_to_tray($window, $event) {
if ($event->changed_mask == Gdk::WINDOW_STATE_ICONIFIED &&
$event->new_window_state & Gdk::WINDOW_STATE_ICONIFIED) {
$window->hide();
}
return true; //stop bubbling
}
public function update_public_timeline() {
$this->pic_queue = array();
$list = $this->twitter->get_public_timeline();
$this->statusbar->pop(1);
$this->statusbar->push(1, 'last updated ' . date('Y-m-d H:i') . ' - ' . count($list) . ' new tweets');
$store = $this->treeview->get_model();
$store->clear();
foreach($list as $object) {
$store->append(array(null, $object->user->profile_image_url, $object->user->name,
$object->user->id, $object->text, $object->favorited, $object->created_at,
$object->id));
}
return true;
}
public function update_timeline() {
$list = $this->twitter->get_timeline();
$this->statusbar->pop(1);
$this->statusbar->push(1, 'last updated ' . date('Y-m-d H:i') . ' - ' . count($list) . ' new tweets');
$store = $this->treeview->get_model();
foreach($list as $object) {
$store->append(array(null, $object->user->profile_image_url, $object->user->name,
$object->user->id, $object->text, $object->favorited, $object->created_at,
$object->id));
}
return true;
}
public function login() {
if (!empty($this->load_images_timeout)) {
Gtk::timeout_remove($this->load_images_timeout);
$readd = true;
}
Gtk::timeout_remove($this->public_timeline_timeout);
$login = new Login($this);
while($response = $login->run()) {
if ($response == GTK::RESPONSE_CANCEL || $response == GTK::RESPONSE_DELETE_EVENT) {
if (isset($readd)) {
$this->load_images_timeout = Gtk::timeout_add(500, array($this, 'pic_queue'));
}
$this->public_timeline_timeout = Gtk::timeout_add(61000, array($this, 'update_public_timeline')); // every 60 seconds
$login->destroy();
break;
} elseif ($response == GTK::RESPONSE_OK) {
if($login->check_login($this->twitter)) {
$this->logoutbutton->set_sensitive(true);
$this->loginbutton->set_sensitive(false);
$login->destroy();
$this->public_timeline_timeout = Gtk::timeout_add(61000, array($this, 'update_timeline')); // every 60 seconds
$this->load_images_timeout = Gtk::timeout_add(500, array($this, 'pic_queue'));
$this->treeview->get_model()->clear();
$this->pic_queue = array();
$this->pic_cached = array();
$this->update_timeline();
$this->updateentry->set_sensitive(true);
break;
}
}
}
}
public function logout() {
$this->twitter->logout();
$this->logoutbutton->set_sensitive(false);
$this->loginbutton->set_sensitive(true);
$this->public_timeline_timeout = Gtk::timeout_add(61000, array($this, 'update_public_timeline')); // every 60 seconds
$this->pic_queue = array();
$this->pic_cached = array();
$this->update_public_timeline();
$this->updateentry->set_sensitive(false);
}
public function send_update($entry) {
if ($this->twitter->send($entry->get_text())) {
$this->entrystatus->set_text('Message Sent');
$this->update_timeline();
$this->updateentry->set_text('');
} else {
$this->entrystatus->set_markup('<span color="red">Error Sending Message - Try Again</span>');
}
}
public function __destruct() {
foreach(scandir($this->temp) as $filename) {
if ($filename[0] == '.')
continue;
if (file_exists($this->temp . $filename)) {
unlink($this->temp . $filename);
}
}
}
}