~rick-rickspencer3/lolz/quickly_trunk

25 by Rick Spencer
added microblog capabilities
1
# -*- coding: utf-8 -*-
2
### BEGIN LICENSE
26 by Rick Spencer
quickly released
3
# Copyright (C) 2009 Rick Spencer rick.spencer@canonical.com
4
#This program is free software: you can redistribute it and/or modify it 
5
#under the terms of the GNU General Public License version 3, as published 
6
#by the Free Software Foundation.
7
#
8
#This program is distributed in the hope that it will be useful, but 
9
#WITHOUT ANY WARRANTY; without even the implied warranties of 
10
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
11
#PURPOSE.  See the GNU General Public License for more details.
12
#
13
#You should have received a copy of the GNU General Public License along 
14
#with this program.  If not, see <http://www.gnu.org/licenses/>.
25 by Rick Spencer
added microblog capabilities
15
### END LICENSE
16
17
import sys
18
import os
19
import gtk
20
import logging
21
import dbus
22
import time
23
24
from lolz.lolzconfig import getdatapath
25
26
try:
27
    bus = dbus.SessionBus()
28
    db_mb_obj = bus.get_object("com.Gwibber", "/com/gwibber/Microblog")
29
    microblog = dbus.Interface(db_mb_obj, "com.Gwibber")
30
    short_obj = bus.get_object("com.Gwibber","/com/gwibber/URLShorten")
31
    url_shorter = dbus.Interface(short_obj,"com.Gwibber")
32
    import gwibber
33
    import gwibber.config
34
35
36
except Exception, inst:
37
    print inst
38
    logging.debug("Failed to setup dbus connection to gwibber")
39
40
class MicroblogDialog(gtk.Dialog):
41
    __gtype_name__ = "MicroblogDialog"
42
43
    def __init__(self):
44
        """__init__ - This function is typically not called directly.
45
        Creation of a MicroblogDialog requires redeading the associated ui
46
        file and parsing the ui definition extrenally, 
47
        and then calling MicroblogDialog.finish_initializing().
48
    
49
        Use the convenience function NewMicroblogDialog to create 
50
        a MicroblogDialog object.
51
    
52
        """
53
        pass
54
55
    def finish_initializing(self, builder, url):
56
        """finish_initalizing should be called after parsing the ui definition
57
        and creating a MicroblogDialog object with it in order to finish
58
        initializing the start of the new MicroblogDialog instance.
59
    
60
        """
61
        #get a reference to the builder and set up the signals
62
        self.builder = builder
63
        self.builder.connect_signals(self)
64
        short = url_shorter.shorten(url, "is.gd")
65
        self.builder.get_object("entry1").set_text("lolz " + short)
66
67
    def ok(self, widget, data=None):
68
        """ok - The user has elected to save the changes.
69
        Called before the dialog returns gtk.RESONSE_OK from run().
70
71
        """
72
        try:
73
            blog_text = self.builder.get_object("entry1").get_text()
74
            if len(blog_text) > 140:
75
                logging.debug('ERROR: blog_text too long' + blog_text)
76
                return
77
78
            accounts = gwibber.config.Accounts()
79
80
            args = [blog_text]
81
            for account in accounts:
82
                if account["send_enabled"]:
83
                    acctid = account["id"]
84
                microblog.operation({
85
                      "source": "lolz",
86
                      "id": "send-%s-%s" % (acctid, time.time()),
87
                      "accountid": acctid,
88
                      "args": args,
89
                      "opname": "send",
90
                      })
91
            widget.set_sensitive(False)            
92
            logging.debug("sent through gwibber: " + blog_text)
93
94
        except Exception, inst:
95
            print inst
96
            logging.debug(str(inst))
97
98
    def cancel(self, widget, data=None):
99
        """cancel - The user has elected cancel changes.
100
        Called before the dialog returns gtk.RESPONSE_CANCEL for run()
101
102
        """         
103
        pass
104
105
def NewMicroblogDialog(url):
106
    """NewMicroblogDialog - returns a fully instantiated
107
    dialog-camel_case_nameDialog object. Use this function rather than
108
    creating MicroblogDialog instance directly.
109
    
110
    """
111
112
    #look for the ui file that describes the ui
113
    ui_filename = os.path.join(getdatapath(), 'ui', 'MicroblogDialog.ui')
114
    if not os.path.exists(ui_filename):
115
        ui_filename = None
116
117
    builder = gtk.Builder()
118
    builder.add_from_file(ui_filename)    
119
    dialog = builder.get_object("microblog_dialog")
120
    dialog.finish_initializing(builder, url)
121
    return dialog
122
123
if __name__ == "__main__":
124
    dialog = NewMicroblogDialog()
125
    dialog.show()
126
    gtk.main()
127