File: blog_poster.py

package info (click to toggle)
gnome-blog 0.8-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,172 kB
  • ctags: 144
  • sloc: sh: 3,172; python: 1,144; makefile: 131; xml: 8
file content (171 lines) | stat: -rw-r--r-- 5,945 bytes parent folder | download
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
import gtk
import pango
import gconf

from gettext import gettext as _

from gnomeblog import hig_alert
from gnomeblog import rich_entry
from gnomeblog import blog
from gnomeblog import blogger_prefs

#check if pygtkspell is installed
try:
    import gtkspell
    use_gtkspell = 1
except:
    use_gtkspell = 0

gconf_prefix = None

class BlogPoster(gtk.Frame):
    def __init__(self, prefs_key="/apps/gnome-blog", on_entry_posted=None, accel_group=None):
        gtk.Frame.__init__(self)
        self.set_shadow_type(gtk.SHADOW_OUT)

        self.on_entry_posted = on_entry_posted

        global gconf_prefix
        gconf_prefix = prefs_key

        print "Using gconf_prefix %s" % (gconf_prefix)
            
        box = gtk.VBox()
        box.set_border_width(6)
        box.set_spacing(6)
        
        self.blogEntry   = rich_entry.RichEntry()

	#if we are using gtkspell, attach it to the blogEntry
	if use_gtkspell:
		gtkspell.Spell(self.blogEntry)

        scroller         = gtk.ScrolledWindow()
        self.postButton  = gtk.Button(_("_Post Entry"))
        
        scroller.add(self.blogEntry)
        scroller.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        scroller.set_size_request(400, 300)
        scroller.set_shadow_type(gtk.SHADOW_IN)
        
        self.postButton.connect("clicked", self._onPostButtonClicked)
        self.postButtonAlignment = gtk.Alignment(xalign=1.0, yalign=0.5)
        self.postButtonAlignment.add(self.postButton)

        buttonBox = gtk.HBox()
        buttonBox.set_spacing(6)
        buttonBox.pack_end(self.postButtonAlignment)

        boldToggle   = self.blogEntry.createStyleToggle([("weight", pango.WEIGHT_BOLD)], gtk.STOCK_BOLD, "strong")
        italicToggle = self.blogEntry.createStyleToggle([("style", pango.STYLE_ITALIC)], gtk.STOCK_ITALIC, "em")
        linkButton   = rich_entry.InsertHyperlinkButton(self.blogEntry)
        
        if (accel_group):
            boldToggle.add_accelerator("clicked", accel_group, ord("B"),
                                       gtk.gdk.CONTROL_MASK, 0)
            italicToggle.add_accelerator("clicked", accel_group, ord("I"),
                                         gtk.gdk.CONTROL_MASK, 0)

        self.prefs_button = gtk.Button(_("Preferences..."))
        self.prefs_button.connect("clicked", self._onPrefsButtonClicked)

        buttonBox.pack_start(boldToggle, expand=gtk.FALSE)
        buttonBox.pack_start(italicToggle, expand=gtk.FALSE)        
        buttonBox.pack_start(linkButton, expand=gtk.FALSE)
        buttonBox.pack_start(self.prefs_button, expand=gtk.FALSE)
            
        self.titleEntry = gtk.Entry()

        titleBox = gtk.HBox()
        titleBox.set_spacing(12)
        titleBox.pack_start(gtk.Label(_("Title:")), expand=gtk.FALSE)
        titleBox.pack_start(self.titleEntry)

        box.pack_start(titleBox)
        box.pack_start(scroller)
        box.pack_start(buttonBox)

        self.add(box)
        box.show_all()

        self.titleEntry.connect('activate', lambda entry,box=box: box.child_focus(gtk.DIR_TAB_FORWARD))

        self.titleEntry.connect('changed', self._checkEmptyPost)
        self.blogEntry.buffer.connect('changed', self._checkEmptyPost)
        self._checkEmptyPost()

    def _checkEmptyPost(self, *args):
        sensitive = 1
        if not self.titleEntry.get_text().strip():
            sensitive = 0
        start,end = self.blogEntry.buffer.get_bounds()
        if not start.get_visible_slice(end).strip():
            sensitive = 0
        self.postButton.set_sensitive(sensitive)
        
    def _onPostButtonClicked(self, button):
        global gconf_prefix, appkey

        images = self.blogEntry.getImages()

        try:
            for image in images:
                image.uri = blog.uploadImage(image, gconf_prefix)
                image.opening_tag = '<img src="%s"/>' % (image.uri)
        except blog.FeatureNotSupported, e:
            hig_alert.reportError(_("Couldn't upload images"), _("The blog protocol in use does not support uploading images"))

        #we must turn off the spell checker so as not to confuse
        #the markup to html converter
        if use_gtkspell:
            spell = gtkspell.get_from_text_view(self.blogEntry)
            spell.detach()
        
        html_text = self.blogEntry.getHTML()

	#turn spelling back on
        if use_gtkspell:
            gtkspell.Spell(self.blogEntry)

        print "Text is: {\n %s \n }\n" % (html_text)
        title = self.titleEntry.get_text()

        # Don't post silly blog entries like blank ones
        if not self._postIsReasonable(html_text):
            return

        successful_post = blog.postEntry(title, html_text, gconf_prefix)

        if successful_post:
            # Only delete the entry if posting was successful
            self._clearBlogEntryText()
            # Call back our parent informing them the entry was posted
            if self.on_entry_posted != None:
                self.on_entry_posted()

    def _clearBlogEntryText(self):
        self.blogEntry.clear()
        self.titleEntry.delete_text(0, -1)

    def _onPrefsButtonClicked(self, button):
        self._showPrefDialog()

    def _showPrefDialog(self):
        prefs_dialog = blogger_prefs.BloggerPrefs(gconf_prefix)
        prefs_dialog.show()
        prefs_dialog.run()
        prefs_dialog.hide()

    def _postIsReasonable(self, text):
        # Popup a dialogue confirming even if its deemed
        # unreasonable
        if not text:
            hig_alert.reportError(_("Blog Entry is Blank"), _("No text was entered in the blog entry box. Please enter some text and try again"))
            return gtk.FALSE
        else:
            return gtk.TRUE

class BlogPosterSimple(BlogPoster):
    def __init__(self, prefs_key="/apps/gnome-blog", on_entry_posted=None):    
        BlogPoster.__init__(self,prefs_key,on_entry_posted)
        self.prefs_button.hide_all();