Class: Grumblr::Dashboard

Inherits:
Gtk::VBox
  • Object
show all
Defined in:
lib/grumblr/ui.rb

Instance Method Summary collapse

Constructor Details

#initializeDashboard

Returns a new instance of Dashboard.



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
# File 'lib/grumblr/ui.rb', line 47

def initialize
  super false, 4

  ##
  ### Statusbar
  ##
  @statusbar = Gtk::Statusbar.new

  ##
  ### Notebook
  ##
  @notebook = Gtk::Notebook.new
  @notebook.set_homogeneous true
  @notebook.set_tab_pos Gtk::POS_LEFT

  #
  # Text page
  #
  @text_title = Gtk::Entry.new

  @format = Gtk::CheckButton.new '_markdown'
  @format.set_active $cfg.get(:format_markdown)
  @format.signal_connect(:toggled) do |widget|
    $cfg.set :format_markdown, widget.active?
  end

  label = Gtk::Label.new 'Body'

  box = Gtk::HBox.new false, 8
  box.pack_start label, false
  box.pack_start @format, false

  page = Gtk::VBox.new false, 4
  page.set_border_width 8
  page.pack_with_label 'Title (optional)', @text_title
  page.pack_start box, false
  page.pack_start multiline_entry(:text_body), true

  @notebook.add_page_with_tab page, 'Text'

  #
  # Link page
  #
  @link_url = Gtk::Entry.new
  @link_name = Gtk::Entry.new
  link_description = multiline_entry :link_description

  page = Gtk::VBox.new false, 4
  page.set_border_width 8
  page.pack_with_label 'URL', @link_url
  page.pack_with_label 'Name (optional)', @link_name
  page.pack_with_label'Description (optional)', link_description, true

  @notebook.add_page_with_tab page, 'Link'

  #
  # Chat page
  #
  @chat_title = Gtk::Entry.new
  chat_conversation = multiline_entry :chat_conversation

  page = Gtk::VBox.new false, 4
  page.set_border_width 8
  page.pack_with_label 'Title (optional)', @chat_title
  page.pack_with_label 'Conversation', chat_conversation, true

  @notebook.add_page_with_tab page, 'Chat'

  #
  # Quote page
  #
  @quote_source = Gtk::Entry.new
  quote_quote = multiline_entry :quote_quote

  page = Gtk::VBox.new false, 4
  page.set_border_width 8
  page.pack_with_label 'Quote', quote_quote, true
  page.pack_with_label 'Source (optional)', @quote_source

  @notebook.add_page_with_tab page, 'Quote'

  #
  # Photo page
  #
  filter = Gtk::FileFilter.new
  filter.set_name "Images"
  filter.add_mime_type "image/*"

  @photo_source = Gtk::Entry.new
  @photo_click_through_url = Gtk::Entry.new
  photo_data = file_chooser_button :photo_data, filter
  photo_caption = multiline_entry :photo_caption

  page = Gtk::VBox.new false, 4
  page.set_border_width 8
  page.pack_with_label 'File', photo_data
  page.pack_with_label 'Source', @photo_source
  page.pack_with_label 'Caption', photo_caption, true
  page.pack_with_label 'Link (optional)', @photo_click_through_url

  @notebook.add_page_with_tab page, 'Photo'

  #
  # Audio page
  #
  if $api.user.can_upload_audio == '1'
    filter = Gtk::FileFilter.new
    filter.set_name "Audio"
    filter.add_mime_type "audio/*"

    audio_data = file_chooser_button :audio_data, filter
    audio_caption = multiline_entry :audio_caption

    page = Gtk::VBox.new false, 4
    page.set_border_width 8
    page.pack_with_label 'File', audio_data
    page.pack_with_label 'Caption (optional)', audio_caption, true

    @notebook.add_page_with_tab page, 'Audio'
  end

  #
  # Video page
  #
  @video_embed = Gtk::Entry.new
  video_caption = multiline_entry :video_caption

  page = Gtk::VBox.new false, 4
  page.set_border_width 8

  if $api.user.can_upload_video == '1'
    filter = Gtk::FileFilter.new
    filter.set_name "Video"
    filter.add_mime_type "video/*"

    video_data = file_chooser_button :video_data, filter
    @video_title = Gtk::Entry.new

    page.pack_with_label 'File', video_data
    page.pack_with_label 'Title (optional)', @video_title
  end

  page.pack_with_label 'Embed code / YouTube link', @video_embed
  page.pack_with_label 'Caption (optional)', video_caption, true

  @notebook.add_page_with_tab page, 'Video'

  ##
  ### Toolbar
  ##

  toolbar = Gtk::Toolbar.new
  toolbar.icon_size = Gtk::IconSize::MENU

  icon = Gtk::Image.new Gtk::Stock::HOME, Gtk::IconSize::MENU
  item = Gtk::ToolButton.new icon, 'Tumblelog'
  item.signal_connect(:clicked) do
    Thread.new { system('xdg-open "%s"' % $app.blog.url) }
  end
  toolbar.insert 0, item

  icon = Gtk::Image.new Gtk::Stock::PREFERENCES, Gtk::IconSize::MENU
  item = Gtk::ToolButton.new icon, 'Dashboard'
  item.signal_connect(:clicked) do
    Thread.new { system('xdg-open "http://www.tumblr.com/tumblelog/%s"' % $app.blog.name) }
  end
  toolbar.insert 1, item

  combo = Gtk::ComboBox.new
  active_blog = $cfg.get(:active_blog) || nil
  active_blog_idx = nil
  $api.blogs.each_with_index do |blog, idx|
    combo.append_text blog.title
    active_blog_idx = idx if blog.name.eql?(active_blog)
    active_blog_idx = idx if active_blog_idx.nil? and blog.is_primary == "yes"
  end
  combo.signal_connect(:changed) do |widget|
    $app.blog = $api.blogs[widget.active]
    $cfg.set :active_blog, $app.blog.name
    @statusbar.push 0, $app.blog.title
  end
  combo.set_active(active_blog_idx)
  item =  Gtk::ToolItem.new
  item.set_expand true
  item.add combo
  toolbar.insert 2, item

  icon = Gtk::Image.new Gtk::Stock::QUIT, Gtk::IconSize::MENU
  item = Gtk::ToolButton.new icon, 'Quit'
  item.set_homogeneous false
  item.signal_connect(:clicked) do
    $app.quit
  end
  toolbar.insert 3, item

  ##
  ### Buttons
  ##
  clear_button = Gtk::Button.new 'Clear'
  clear_button.set_focus_on_click false
  clear_button.signal_connect(:clicked) do |widget|
    page = @notebook.get_nth_page @notebook.page
    message_type = @notebook.get_menu_label_text page
    reset_form message_type.downcase
  end

  @private_button = Gtk::ToggleButton.new 'Private'
  @private_button.signal_connect(:toggled) do |widget|
    $cfg.set :private, widget.active?
  end
  @private_button.set_active $cfg.get(:private)

  submit_button = Gtk::Button.new 'Send'
  submit_button.signal_connect(:released) do |widget|
    post
  end

  button_box = Gtk::HBox.new false, 4
  button_box.pack_start clear_button, false
  button_box.pack_start @private_button, false
  button_box.pack_start submit_button

  ##
  ### Layout
  ##
  pack_start toolbar, false
  pack_start @notebook
  pack_start button_box, false
  pack_start @statusbar, false
  show_all
end

Instance Method Details

#collect_data_for(fieldset, message_type) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
# File 'lib/grumblr/ui.rb', line 322

def collect_data_for(fieldset, message_type)
  data = {}
  for key in fieldset[message_type]
    name = "@#{message_type}_#{key.gsub(/-/,'_')}"
    if var = instance_variable_get(name)
      value = var.get_value
      data.merge!({ key => value })
    end
  end
  data
end

#file_chooser_button(name, filter = nil) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/grumblr/ui.rb', line 334

def file_chooser_button(name, filter = nil)
  button = Gtk::FileChooserButton.new('Open', Gtk::FileChooser::ACTION_OPEN)
  if filter
    button.add_filter(filter)
    button.set_filter(filter)
  end
  button.signal_connect(:selection_changed) do |widget|
    puts widget.filename
  end
  button.show_all
  instance_variable_set "@#{name}", button
end

#multiline_entry(name) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/grumblr/ui.rb', line 347

def multiline_entry(name)
  instance_variable_set "@#{name}", Gtk::TextBuffer.new

  view = Gtk::TextView.new
  view.set_buffer instance_variable_get("@#{name}")
  view.set_wrap_mode Gtk::TextTag::WRAP_WORD
  view.set_right_margin 5
  view.set_left_margin 5

  window = Gtk::ScrolledWindow.new
  window.set_shadow_type Gtk::SHADOW_IN
  window.set_policy Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC
  window.add view
  window.show_all
end

#postObject



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
# File 'lib/grumblr/ui.rb', line 279

def post
  page = @notebook.get_nth_page @notebook.page
  message_type = @notebook.get_menu_label_text(page).downcase

  mandatory_data = collect_data_for Ppds::Tumblr::MANDATORY_FIELDS, message_type
  concurent_data = collect_data_for Ppds::Tumblr::CONCURENT_FIELDS, message_type
  optional_data  = collect_data_for Ppds::Tumblr::OPTIONAL_FIELDS,  message_type

  mandatory_data.each do |key, value|
    raise "Mandatory field %s is not set!" % key if not value or value.empty?
  end unless Ppds::Tumblr::MANDATORY_FIELDS[message_type].empty?

  unless Ppds::Tumblr::CONCURENT_FIELDS[message_type].empty?
    concurent_data.delete_if { |x,y| y == "" or y.nil? }
    raise "None of fields %s is set!" % Ppds::Tumblr::CONCURENT_FIELDS[message_type].join(", ") if concurent_data.empty?
  end

  optional_data.delete_if { |x,y| y == "" or y.nil? }

  data = {
    :generator  => 'Grumblr',
    :email      => $cfg.get(:email),
    :password   => $cfg.get(:password),
    :channel_id => $app.blog.name,
    :group      => $app.blog.name + '.tumblr.com',
    :type       => message_type,
    :format     => @format.active? ? 'markdown' : 'html',
    :private    => @private_button.active? ? 1 : 0
  }

  data.merge! mandatory_data
  data.merge! concurent_data
  data.merge! optional_data

  data.update({:data => File.read(data['data'])}) if data.has_key?('data') and data['data'] != ""

  $api.query 'write', data
  MessageDialog.new "Message posted", Gtk::Stock::DIALOG_INFO
  reset_form message_type
rescue Exception
  MessageDialog.new $!
end

#reset_fields_for(fieldset, message_type) ⇒ Object



363
364
365
366
367
368
369
# File 'lib/grumblr/ui.rb', line 363

def reset_fields_for(fieldset, message_type)
  for key in fieldset[message_type]
    name = "@#{message_type}_#{key.gsub(/-/,'_')}"
    var = instance_variable_get(name)
    var.clear if var
  end
end

#reset_form(message_type) ⇒ Object



371
372
373
374
375
376
377
# File 'lib/grumblr/ui.rb', line 371

def reset_form(message_type)
  [ Ppds::Tumblr::MANDATORY_FIELDS,
    Ppds::Tumblr::CONCURENT_FIELDS,
    Ppds::Tumblr::OPTIONAL_FIELDS ].each do |fieldset|
    reset_fields_for(fieldset, message_type)
  end
end