~ubuntu-branches/ubuntu/maverick/awn-extras-applets/maverick

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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
#! /usr/bin/python
#
# Copyright (c) 2009, 2010 sharkbaitbobby <sharkbaitbobby+awn@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import base64
import cPickle as pickle
import cStringIO
import os
import re
import time
import urllib
import urllib2

try:
    import json
except:
    import simplejson as json

import dbus
import dbus.service
import dbus.glib
from desktopagnostic import Color
from desktopagnostic.config import GROUP_DEFAULT
import feedparser
import gtk

from awn import extras
from awn.extras import _, awnlib

pickle_path = '%s/.config/awn/applets/.feeds-tokens' % os.environ['HOME']

twitter_path = extras.PREFIX + '/share/avant-window-navigator/applets/feeds/icons/twitter-16x16.png'

cache_dir = os.environ['HOME'] + '/.cache/awn-feeds-applet'


#A D-Bus service; external apps can tell feeds to update or add a feed
class DBusService(dbus.service.Object):
    def __init__(self, applet):
        self.applet = applet

        bus_name = dbus.service.BusName('org.awnproject.Feeds', bus=dbus.SessionBus())

        dbus.service.Object.__init__(self, bus_name, '/org/awnproject/Feeds')

    @dbus.service.method('org.awnproject.Feeds')
    def Update(self):
        self.applet.update_feeds()

        return 'OK'

    @dbus.service.method('org.awnproject.Feeds')
    def AddFeed(self, url):
        if url not in self.applet.urls:
            self.applet.add_feed(url)

        return 'OK'

class PlaceHolder(gtk.DrawingArea):
    def __init__(self):
        gtk.DrawingArea.__init__(self)

        self.connect('expose-event', self.draw)

    def draw(self, widget, event):
        w = self.allocation.width
        h = self.allocation.height

        gdk_color = self.get_style().text[gtk.STATE_NORMAL].to_string()
        color = Color.from_string(gdk_color + 'ffff')
        cr = self.window.cairo_create()
        cr.set_source_rgba(*color.get_cairo_color())
        cr.set_dash((1, 0, 0, 1))
        cr.rectangle(0.0, 0.0, w, h)
        cr.stroke()

#Used for storing tokens for service logins
#(Also works as a conventient pickle wraparound)
class Tokens:
    def __init__(self, uri=pickle_path):
        self.uri = uri
        fp = None

        try:
            fp = open(uri, 'r')
            self.tokens = pickle.load(fp)
        except:
            self.tokens = {}

        if fp:
            fp.close()

    def set_key(self, key, value):
        self.tokens[key] = value

        fp = open(self.uri, 'w+')
        pickle.dump(self.tokens, fp)
        fp.close()

    def remove_key(self, key):
        del self.tokens[key]

        fp = open(self.uri, 'w+')
        pickle.dump(self.tokens, fp)
        fp.close()

    def __getitem__(self, key):
        try:
            return self.tokens[key]
        except:
            return None

    def __setitem__(self, key, value):
        return self.set_key(key, value)

    def __iter__(self):
        return self.tokens.__iter__()

#Just to standardize everything
class Entry(dict):
    def __init__(self, url='', title='', new=False, notify=False):
        dict.__init__(self)
        self['url'] = url
        self['title'] = title
        self['new'] = new
        self['notify'] = notify

    def basic(self):
        return {'url': self['url'], 'title': self['title']}

#Base class for all types of sources
class FeedSource:
    io_error = False
    login_error = False
    applet = None
    icon = None
    title = ''
    base_id = '' #e.g. google-reader
    url = '' #         google-reader-username
    web_url = '' #     http://www.google.com/reader/
    entries = []
    num_new = 0
    num_notify = 0

    def __init__(self):
        pass

    def error(self, *args):
        self.io_error = True

        self.applet.got_favicon(self, True)

        self.applet.feed_updated(self)

    def update(self):
        self.applet.feed_updated(self)

    def delete(self):
        if self.url in self.applet.tokens:
            self.applet.tokens.remove_key(self.url)

    def callback(self, *args):
        pass

    def post_data(self, uri,
                  headers={},
                  data=None,
                  timeout=30,
                  server_headers=False,
                  opener=None,
                  cb=None,
                  error_cb=None):
        if cb is None:
            cb = self.callback

        if error_cb is None:
            error_cb = self.error

        if self.applet:
            self.applet.network_handler.post_data(uri, headers, data, timeout, server_headers, opener, \
                callback=cb, error=error_cb)

    #Also convenience
    def get_data(self, uri,
                 headers={},
                 parse=False,
                 timeout=30,
                 user_data=None,
                 _opener=None,
                 cb=None,
                 error_cb=None):
        if cb is None:
            cb = self.callback

        if error_cb is None:
            error_cb = self.error

        if self.applet:
            self.applet.network_handler.get_data(uri, headers, parse, timeout, \
                user_data=user_data, opener=_opener, callback=cb, error=error_cb)

    def get_favicon(self, siteid=None, url=None):
        #web_url is used by default because many sites use, e.g., rss.example.com,
        #instead of just example.com, and these might not have favicons.
        if siteid is None:
            siteid = self.web_url.replace('http://', '').replace('https://', '')
            siteid = siteid.split('/')[0]

        if url is None:
            url = 'http://' + siteid + '/favicon.ico'

        self._favicon_siteid = siteid

        #Twitter's favicon is ugly, so override it with an included one
        #(This is the icon that the Twitter feed source uses)

        if siteid == 'twitter.com':
            self.icon = twitter_path
            self.applet.got_favicon(self)

            return

        if siteid in self.applet.favicons:
            #Check if the favion is less than a week old
            if self.applet.favicons[siteid] + 604800L > long(time.time()):
                self.icon = siteid
                self.applet.got_favicon(self)

                return

        #Either the icon does not exist or it's more than a week old; fetch it
        self.get_data(url, timeout=15, cb=self.got_favicon, error_cb=self.callback)

    def got_favicon(self, data):
        self.applet.favicons[self._favicon_siteid] = long(time.time())

        fp = open(os.path.join(cache_dir, self._favicon_siteid + '.ico'), 'w+')
        fp.write(data)
        fp.close()

        self.icon = os.path.join(cache_dir, self._favicon_siteid + '.ico')
        self.applet.got_favicon(self)

    #Do something if the feed icon was clicked.
    def icon_clicked(self):
        pass

    #Do something if an item was clicked.
    def item_clicked(self, i):
        pass

#TODO: Still need a better name. This is used if the feed may have items that are not considered new.
class StandardNew:
    newest = None
    last_new = []
    notified = []

    #Call this after getting the entries, but before calling applet.feed_updated()
    def get_new(self):
        new_new = []

        if not self.applet.client.get_bool(GROUP_DEFAULT, 'keep_unread'):
            self.last_new = []

        #See if the feed was updated, etc...
        if self.newest is not None and self.newest['url'] != self.entries[0]['url'] and \
          self.newest['title'] != self.entries[0]['title']:
            got_it = False
            for i, entry in enumerate(self.entries):
                self.num_new = i
                if self.newest['url'] == entry['url'] and self.newest['title'] == entry['title']:
                    break

                else:
                    self.num_new += 1

        if len(self.entries) > 0:
            self.newest = self.entries[0]

        self.num_notify = 0

        #Mark the new feeds as new
        for i, entry in enumerate(self.entries):
            entry['new'] = bool(i < self.num_new) or entry.basic() in self.last_new

            if entry['new']:
                new_new.append(entry.basic())

                if not i < self.num_new:
                    self.num_new += 1

                if [entry['url'], entry['title']] not in self.notified:
                    self.notified.append([entry['url'], entry['title']])
                    entry['notify'] = True
                    self.num_notify += 1

                else:
                    entry['notify'] = False

        self.last_new = new_new

#Used for logging in
class KeySaver:
    key = None
    password = None

    def get_key(self, username, password, url=None):
        if url is None:
            url = self.url

        if self.key is None:
            if not self.applet.keyring:
                self.applet.keyring = awnlib.Keyring()

            token = self.applet.tokens[url]

            #Username and password provided, e.g. from the add feed dialog
            if username and password:
                self.password = password
                if token is None or token == 0:
                    #No for i18n because if the user changes the language, he
                    #could lose the password (and most users won't even see this)
                    self.key = self.applet.keyring.new('Feeds - ' + url,
                        password,
                        {'username': username, 'network': self.base_id},
                        'network')

                    self.applet.tokens[url] = int(self.key.token)

                else:
                    self.key = self.applet.keyring.from_token(token)

            #No password provided, e.g. on applet startup
            else:
                if token is None or token == 0:
                    self.key = None
                    self.error()

                else:
                    self.key = self.applet.keyring.from_token(token)
                    self.password = self.key.password

        return self.key

#Store the passwords together
class GoogleFeed(KeySaver):
    opener = None
    SID = None
    Auth = None
    logged_in = False
    title = _("Google")
    client_login_url = 'https://www.google.com/accounts/ClientLogin'
    service_login_url = 'https://www.google.com/accounts/ServiceLogin'
    service_login_auth_url = 'https://www.google.com/accounts/ServiceLoginAuth'
    feed_search_url = 'http://www.google.com/reader/directory/search?'

    #Override the identifying URL to just be google-username, instead of
    #e.g. google-reader-username, google-wave-username, etc. so that only one has to be stored
    def get_key(self, username, password):
        return KeySaver.get_key(self, username, password, 'google-' + username)

    #Login, emulating the standard Web login
    def service_login(self):
        #Unify logging in to Google across Google services
        if self.username in self.applet.google_logins:
            self.applet.google_logins[self.username]['count'] += 1

        else:
            self.applet.google_logins[self.username] = {'count': 1, 'opener': None, 'SID': None,
                'keyring': self.get_key(self.username, self.password), 'service_logged_in': False}

        if self.key is not None:
            if not self.logged_in:
                if self.opener is None:
                    if self.applet.google_logins[self.username]['opener'] is None:
                        cookies = urllib2.HTTPCookieProcessor()
                        self.opener = urllib2.build_opener(cookies)
                        self.applet.google_logins[self.username]['opener'] = self.opener

                    else:
                        self.opener = self.applet.google_logins[self.username]['opener']

                        if self.applet.google_logins[self.username]['service_logged_in']:
                            if self.should_update:
                                self.update()
                                self.should_update = False

                            return

                self.get_data(self.service_login_url,
                              _opener=self.opener,
                              cb=self.did_service_login,
                              error_cb=self.login_error)

    def did_service_login(self, data):
        galx_match_obj = re.search(r'name="GALX"\s*value="([^"]+)"', data, re.IGNORECASE)
        galx_value = galx_match_obj.group(1) if galx_match_obj.group(1) is not None else ''
        data = urllib.urlencode({'Email': self.username,
            'Passwd': self.password,
            'GALX': galx_value})

        #Now authenticate
        self.post_data(self.service_login_auth_url,
                       {},
                       data,
                       opener=self.opener,
                       cb=self.did_auth,
                       error_cb=self.login_error)

    def did_auth(self, data):
        if data.find("Sign in to") != -1 or data.find("incorrect") != -1:
            self.error()
            self.logged_in = False
            return

        self.logged_in = True
        self.applet.google_logins[self.username]['service_logged_in'] = True

        if self.should_update:
            self.update()
            self.should_update = False

    def login_error(self, *args):
        self.init_network_error = True
        self.error()

    #This is a special login used for APIs, such as gdata.
    def client_login(self, service):
        #Unify logging in to Google across Google services
        if self.username in self.applet.google_logins:
            self.applet.google_logins[self.username]['count'] += 1

        else:
            self.applet.google_logins[self.username] = {'count': 1, 'opener': None, 'SID': None,
                'keyring': self.get_key(self.username, self.password), 'service_logged_in': False}

        if self.key is not None:
            #Get the magic SID from Google to login, if we haven't already
            if self.SID is None:
                if self.applet.google_logins[self.username]['SID'] is not None:
                    self.SID = self.applet.google_logins[self.username]['SID']

                    if self.should_update:
                        self.update()
                        self.should_update = False

                else:
                    #Format the request
                    data = urllib.urlencode({'service': service,
                        'Email': self.username,
                        'Passwd': self.password,
                        'source': 'awn-feeds-applet-' + extras.__version__,
                        'continue': 'http://www.google.com/'})

                    #Send the data to get the SID
                    self.post_data(self.client_login_url, {}, data, 15,
                        cb=self.got_sid, error_cb=self.login_error)

    def got_sid(self, data):
        self.init_network_error = False

        #Check if wrong password/username
        if data.find('BadAuthentication') != -1:
            self.error()
            return

        #Save the SID so we don't have to re-login every update
        for line in data.split('\n'):
            if line.find('SID=') == 0:
                self.SID = line.split('=')[1]
            elif line.find('Auth=') == 0:
                self.Auth = line.split('=')[1]

        self.logged_in = True

        if self.should_update:
            self.update()
            self.should_update = False

    def get_search_results(self, query, cb, _error_cb):
        search_url = self.feed_search_url + urllib.urlencode({'q': query})

        if None not in (self.SID, self.Auth):
            self.get_data(search_url, {'Cookie': 'SID=' + self.SID}, False,
                user_data=(cb, _error_cb), cb=self.got_search_results, error_cb=_error_cb)

        elif self.opener is not None:
            self.get_data(search_url, user_data=(cb, _error_cb), _opener=self.opener,
                cb=self.got_search_results, error_cb=_error_cb)

    def got_search_results(self, cbs, data):
        cb, error_cb = cbs

        try:
            data = data.split('_DIRECTORY_SEARCH_DATA =')[1].split('</script>')[0].strip()
            data = json.loads(data)

            results = data['results']

            feeds = []
            for result in results:
                #For some reason 'streamid' starts with 'feed/'
                url = result['streamid'][5:]
                feeds.append({'url': url, 'title': result['title']})

        except:
            error_cb()

        else:
            cb(feeds)


#Thank you http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI
class GoogleReader(FeedSource, StandardNew, GoogleFeed):
    base_id = 'google-reader'
    web_url = 'http://www.google.com/reader/'
    title = _("Google Reader")
    fetch_url = 'http://www.google.com/reader/atom/user/-/state/com.google/reading-list?n=5'
    favicon_url = 'http://www.google.com/reader/ui/favicon.ico'

    def __init__(self, applet, username, password=None):
        self.applet = applet
        self.username = username
        self.url = self.base_id + '-' + username

        self.should_update = False
        self.init_network_error = False

        #Get ready to update/fetch feed, but don't actually do so.
        self.get_key(username, password)
        self.client_login('reader')
        self.get_favicon('google-reader', self.favicon_url)

    #Update the Google Reader feed
    def update(self):
        if self.SID is None and not self.init_network_error:
            self.should_update = True

        elif self.init_network_error:
            self.should_update = True
            self.client_login('reader')

        else:
            #Load the reading list with that magic SID as a cookie
            self.get_data(self.fetch_url, {'Cookie': 'SID=' + self.SID,
                'Authorization': 'GoogleLogin auth=' + self.Auth}, True, cb=self.got_parsed)

    def got_parsed(self, parsed):
        self.entries = []
        for entry in parsed.entries[:5]:
            self.entries.append(Entry(entry.link, entry.title))

        self.get_new()

        self.applet.feed_updated(self)
        self.get_favicon('google-reader', self.favicon_url)

#Code largely based on work in googsystray by Jim Duchek
#http://googsystray.sourceforge.net/
class GoogleWave(FeedSource, GoogleFeed):
    base_id = 'google-wave'
    web_url = 'http://wave.google.com/wave/'
    title = _("Google Wave")
    fetch_url = 'https://wave.google.com/wave/'

    def __init__(self, applet, username, password=None):
        self.applet = applet
        self.username = username
        self.url = self.base_id + '-' + username

        self.should_update = False
        self.init_network_error = False

        self.notified_about = []

        #Get ready to update/fetch feed, but don't actually do so.
        self.get_key(username, password)
        self.service_login()
        self.get_favicon('wave.google.com')

    def update(self):
        if not self.logged_in and not self.init_network_error:
            self.should_update = True

        #This happens if the user was not connected at startup
        #E.g. on Wifi, very common.
        elif self.init_network_error:
            self.should_update = True
            self.service_login()

        else:
            self.get_data(self.fetch_url, _opener=self.opener, cb=self.got_data)

    #Loaded the Wave data; now parse
    def got_data(self, data):
        c_start = data.find("userProfile:")
        id_start = data.find("id:'", c_start)
        id_end = data.find("'", id_start + 4)
        username_start = data.find("username:'", c_start)
        username_end = data.find("'", username_start + 10)
        username = data[username_start + 10:username_end]
        id = data[id_start + 4:id_end]

        c_end = 0
        c_start = data.find("var json = ", c_end)

        while c_start != -1:
            c_end = data.find("}};\n", c_start)
            jdata = json.loads(data[c_start + 11:c_end + 2])
            if jdata["t"] == 2602:
                self.check_jdata(jdata)
                break

            c_start = data.find("var json =", c_end)

        self.applet.feed_updated(self)
        self.get_favicon('wave.google.com')

    def check_jdata(self, data):
        waves = data["p"]["1"]
        self.entries = []
        self.num_notify = 0

        for i in waves:
            notify = False

            try:
                if i["7"] == 0:
                    continue
            except:
                continue

            #i['1'] is an unique ID for each wave.
            if i['1'] not in self.notified_about:
              notify = True
              self.num_notify += 1
              self.notified_about.append(i['1'])

            url = 'http://wave.google.com/wave/#minimized:search,restored:wave:'
            url += i['1'].replace('+', '%2B')
            entry = Entry(url, i['9']['1'], True, notify)
            self.entries.append(entry)

        self.num_new = len(self.entries)

class Reddit(FeedSource, KeySaver):
    base_id = 'reddit'
    web_url = 'http://www.reddit.com/message/inbox/'
    title = _("Reddit Inbox")
    orangered_url = 'http://www.reddit.com/static/mail.png'
    login = 'https://www.reddit.com/api/login/%s' # % username
    messages_url = 'http://www.reddit.com/message/inbox/.json?mark=false'
    inbox_url = 'http://www.reddit.com/message/messages/'

    def __init__(self, applet, username, password=None):
        self.applet = applet
        self.username = username
        self.password = password
        self.url = self.base_id + '-' + username

        self.cookie = None
        self.should_update = False
        self.already_notified_about = []
        self.marked_read = []
        self.init_network_error = False

        #Get ready to update the feed, but don't actually do so.
        self.get_key(username, password)
        self.get_favicon('www.reddit.com')
        self.get_reddit_cookie()

    def get_reddit_cookie(self):
        if self.key is not None:
            if self.cookie is None:
                try:
                    data = urllib.urlencode({'user': self.username.lower(),
                        'passwd': self.password,
                        'op': 'login-main',
                        'id': '#login_login-main',
                        'r': 'reddit.com'})

                except:
                    self.error()

                else:
                    self.post_data(self.login % self.username.lower(), {}, data,
                        server_headers = True, cb=self.got_reddit_cookie, error_cb=self.cookie_error)

    def cookie_error(self, *args):
        self.init_network_error = True
        self.error()

    def got_reddit_cookie(self, data, headers):
        self.init_network_error = False
        headers = str(headers).split('\n')

        #Save the cookie
        for line in headers:
            if line.find('Set-Cookie: ') == 0:
                cookie = urllib.unquote(line[12:].split(';')[0])

                #This happens if the username/password is wrong
                if cookie.find('reddit_first=') == 0:
                    self.error()
                    return

                self.cookie = urllib.unquote(line[12:].split(';')[0])

        if self.cookie is None:
            self.error()

        elif self.should_update:
            self.update()
            self.should_update = False

    def update(self):
        if self.cookie is None and not self.init_network_error:
            self.should_update = True

        elif self.init_network_error:
            self.should_update = True
            self.get_reddit_cookie()

        else:
            #Load the reading list with that magic SID as a cookie
            self.get_data(self.messages_url, {'Cookie': self.cookie}, cb=self.got_data)

    def got_data(self, data):
        try:
            parsed = json.loads(data)
        except:
            self.error()
            return

        self.entries = []
        self.num_new = 0
        self.num_notify = 0

        for message in parsed['data']['children'][:5]:
            #If it's a private message
            if message['data']['context'].strip() == '':
                url = self.inbox_url
                title = message['data']['subject']

            else:
                url = 'http://www.reddit.com' + message['data']['context']

                if message['data']['subject'] == 'comment reply':
                    title = _("Comment reply from %s") % message['data']['author']

                else:
                    title = _("Post reply from %s") % message['data']['author']

            new = message['data']['id'] not in self.marked_read and message['data']['new']

            if new:
                self.num_new += 1

            notify = False
            if new and message['data']['id'] not in self.already_notified_about:
                self.already_notified_about.append(message['data']['id'])
                notify = True
                self.num_notify += 1

            entry = Entry(url, title, new, notify)
            entry['id'] = message['data']['id']
            self.entries.append(entry)

        self.applet.feed_updated(self)

        if self.num_new > 0:
            self.get_favicon('www.reddit.com-orangered', self.orangered_url)

        else:
            self.get_favicon('www.reddit.com')

    #If an item was clicked and it was the only unread one,
    #tell Reddit that we've read every message
    #Unfortunately, we can only mark all messages as read, not individual ones.
    def item_clicked(self, i):
        if self.entries[i]['new'] == True:
            self.entries[i]['new'] = False
            self.num_new -= 1
            deboldify(self.applet.displays[self.url].get_children()[i], True)

            self.marked_read.append(self.entries[i]['id'])

            if self.num_new == 0:
                 deboldify(self.applet.feed_labels[self.url])
                 self.get_favicon('www.reddit.com')

class Twitter(FeedSource, StandardNew, KeySaver):
    base_id = 'twitter'
    web_url = 'http://twitter.com/'
    title = _("Twitter")
    timeline_url = 'https://twitter.com/statuses/friends_timeline.rss'
    replies_url = 'https://twitter.com/statuses/replies.rss'

    def __init__(self, applet, username, password=None, base_url='twitter-timeline'):
        self.applet = applet
        self.username = username
        self.url = base_url + '-' + username

        self.get_favicon('twitter.com')

        try:
            self.get_key(username, password)
            self.auth = base64.encodestring(username + ':' + self.password)
            self.auth = {'Authorization': 'Basic ' + self.auth}

        except:
            self.error()

        if base_url.find('twitter-timeline') == 0:
            self.use_timeline = True
            self.use_replies = False

        elif base_url.find('twitter-both') == 0:
            self.use_timeline = True
            self.use_replies = True

        else:
            self.use_timeline = False
            self.use_replies = True

    def update(self):
        if self.use_timeline and self.use_replies:
            self.have_timeline = False
            self.have_replies = False

        if self.use_timeline:
            self.get_data(self.timeline_url, self.auth, True, cb=self.got_timeline)

        if self.use_replies:
            self.get_data(self.replies_url, self.auth, True, cb=self.got_replies)

    def got_timeline(self, parsed):
        self.timeline = parsed

        if self.use_timeline and self.use_replies:
            self.have_timeline = True

            if self.have_replies:
                self.do_entries()

        else:
            self.do_entries()

    def got_replies(self, parsed):
        self.replies = parsed

        if self.use_timeline and self.use_replies:
            self.have_replies = True

            if self.have_timeline:
                self.do_entries()

        else:
            self.do_entries()

    def do_entries(self):
        self.entries = []
        urls = []

        if self.use_timeline:
            for entry in self.timeline.entries:
                parsed_entry = Entry(entry.link, entry.title)
                parsed_entry['time'] = entry.updated_parsed

                urls.append(entry.link)
                self.entries.append(parsed_entry)

        if self.use_replies:
            for entry in self.replies.entries:
                #Don't add a reply if it's in the user's timeline
                #Many (if not most) replies will also be in the user's timeline
                if entry.link not in urls:
                    parsed_entry = Entry(entry.link, entry.title)
                    parsed_entry['time'] = entry.updated_parsed

                    self.entries.append(parsed_entry)

        #Sort the (combined) feeds, newest first
        self.entries.sort(lambda a, b: cmp(b['time'], a['time']))

        self.entries = self.entries[:5]

        self.get_new()
        self.applet.feed_updated(self)
        self.applet.got_favicon(self)

class WebFeed(FeedSource, StandardNew):
    fetched = False
    parsed = None

    #Parsed is not None if a link to a feed is dragged onto the dialog. When that happens,
    #the link is downloaded and parsed. If it is a valid feed, the feedparser-parsed object
    #comes here, so we don't have to download and parse it a second time.
    def __init__(self, applet, url, parsed=None):
        self.applet = applet
        self.base_id = self.url = url
        self.parsed = parsed

        if parsed is not None:
            self.fetched = True

    def update(self):
        if self.fetched:
            self.got_parsed(self.parsed)
            self.fetched = False
            self.parsed = None

        else:
            self.get_data(self.url, parse=True, cb=self.got_parsed)

    def got_parsed(self, parsed):
        self.entries = []

        try:
            self.title = parsed.feed.title
        except:
            self.title = _("Untitled")

        try:
            self.web_url = parsed.feed.link
            if self.web_url[0] == '/':
                self.web_url = '/'.join(self.url.split('/')[:3]) + self.web_url
        except:
            self.web_url = ''

        try:
            for entry in parsed.entries[:5]:
                if entry.link[0] == '/':
                    self.entries.append(Entry('/'.join(self.web_url.split('/')[:3]) + entry.link,
                        entry.title))
                else:
                    self.entries.append(Entry(entry.link, entry.title))
        except:
            self.error()
            return

        self.get_new()

        self.applet.feed_updated(self)

        self.get_favicon()


def get_16x16(pb):
    if pb.get_width() != 16 or pb.get_height() != 16:
        pb2 = pb.scale_simple(16, 16, gtk.gdk.INTERP_BILINEAR)
        del pb
        pb = pb2

    return pb

def boldify(widget, button=False):
    if button:
        widget = widget.child

    widget.set_markup('<span font_weight="bold">%s</span>' % widget.get_text())

def deboldify(widget, button=False):
    if button:
        widget = widget.child

    widget.set_markup(widget.get_text())

def safify(text):
    text = text.replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;')
    text2 = list(text)
    for char, i in enumerate(text2):
        if char == '&':
            ok = False
            if len(text2) >= i:
                if text2[i + 1] == '#' and (text2[i + 4] == ';' or text2[i + 6] == ';'):
                    ok = True
                elif len(text2) - 1 >= i + 3:
                    if text2[i + 1:3] == 'lt;':
                        ok = True
                    elif text2[i + 1:3] == 'gt;':
                        ok = True
                    elif len(text2) -1 >= i + 4:
                        if text2[i + 1:4] == 'quot;':
                            ok = True

            if not ok:
                text2[i] = '&amp;'

    return ''.join(text2)