Class: GloudApp::App

Inherits:
Object
  • Object
show all
Defined in:
lib/gloudapp.rb

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gloudapp.rb', line 13

def initialize
  @client = CloudApp::Client.new

  @credentials = load_credentials
  @client.authenticate(@credentials[:username], @credentials[:password])

  if not credentials_valid?
    @credentials = request_credentials
    @client.authenticate(@credentials[:username], @credentials[:password])
  end

  if not credentials_valid?
    ErrorDialog.run!("Error", "Authentication failed!")
  end

  create_tray
end

Instance Method Details

#check_clipboard(item) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gloudapp.rb', line 103

def check_clipboard(item)
  with_clipboard_text do |text|
    if !text.nil? and File.file?(text)
      item.set_sensitive(true)
      item.label = "Upload: #{text}"
    else
      item.set_sensitive(false)
      item.label = "Upload from clipboard"
    end
  end
end

#check_last_drop(item) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/gloudapp.rb', line 126

def check_last_drop(item)
  if @last_drop.nil?
    item.set_sensitive(false)
    item.label = "Copy last drop url"
  else
    item.set_sensitive(true)
    item.label = "Copy #{@last_drop.url}"
  end
end

#clipboard_text=(text) ⇒ Object



146
147
148
# File 'lib/gloudapp.rb', line 146

def clipboard_text=(text)
  Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD).text = text
end

#copy_last_drop_urlObject



136
137
138
# File 'lib/gloudapp.rb', line 136

def copy_last_drop_url
  self.clipboard_text = @last_drop.url unless @last_drop.nil?
end

#create_trayObject



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
# File 'lib/gloudapp.rb', line 73

def create_tray
  @tray = Tray.new :default => Proc.new { take_screenshot }

  @tray.add_action "Copy last drop url",
    :show => Proc.new { |item| check_last_drop(item) },
    :action => Proc.new { copy_last_drop_url },
    :no_icon_change => true

  @tray.add_separator

  # take and upload screenshot
  @tray.add_action("Take screenshot") { take_screenshot }

  # upload file from path in clipboard
  @tray.add_action "Upload from clipboard",
    :show => Proc.new { |item| check_clipboard(item) },
    :action => Proc.new { upload_from_clipboard }

  # upload file via file chooser
  @tray.add_action("Upload file") { upload_via_chooser }

  # show about dialog
  @tray.add_action("About", :no_icon_change => true) { GloudApp::AboutDialog.run! }

  @tray.add_separator

  # quit app
  @tray.add_action("Quit", :no_icon_change => true) { Gtk.main_quit }
end

#credentials_valid?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
# File 'lib/gloudapp.rb', line 36

def credentials_valid?
  # check whether auth was successful
  @acc = CloudApp::.find
  $domain = @acc.domain.nil? ? 'cl.ly' : @acc.domain
  return true
rescue
  return false
end

#error(message) ⇒ Object



205
206
207
208
209
210
211
212
213
# File 'lib/gloudapp.rb', line 205

def error(message)
  options = {:message => message} unless message.is_a?(Hash)
  options = {:title => 'Error'}.merge(options)

  @tray.icon = Icon.error
  @tray.message = options[:message]
  ErrorDialog.run!(options[:title], options[:message])
  false
end

#load_credentialsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gloudapp.rb', line 45

def load_credentials
  if ARGV.length == 2
    # assume that's username and password in ARGV
    return {:username => ARGV[0], :password => ARGV[1]}
  end

  @config_file = File.join(ENV['HOME'], '.cloudapp-cli')
  if File.exists?(@config_file)
    creds = YAML.load_file(@config_file)
    return creds unless creds[:username].nil? or creds[:password].nil?
  end

  request_credentails
end

#request_credentialsObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gloudapp.rb', line 60

def request_credentials
   = LoginDialog.new
  case .run
  when Gtk::Dialog::RESPONSE_ACCEPT
    creds = {:username => ..text, :password => .password.text}
    .destroy
    return creds
  when Gtk::Dialog::RESPONSE_REJECT
    .destroy
    return nil
  end
end

#run!Object



31
32
33
34
# File 'lib/gloudapp.rb', line 31

def run!
  @tray.run!
  Gtk.main
end

#take_screenshotObject



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/gloudapp.rb', line 192

def take_screenshot
  file = File.join(TMP_DIR, "Screenshot #{Time.now.strftime(SCRN_TIME_FMT)}.png")
  puts "Taking screenshot..."
  # TODO: find rubish way to take screen shots
  # make screenshot via image magick:
  system("import -window root \"#{file}\"")
  if File.file?(file)
    upload_file(file)
  else
    error "Error taking screenshot - did you install imagemagick?"
  end
end

#upload_file(file) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/gloudapp.rb', line 178

def upload_file(file)
  if File.file?(file)
    puts "Uploading #{file}"
    drop = @client.upload(file)
    puts "URL (in clipboard, too): #{drop.url}"
    # copy URL to clipboard
    self.clipboard_text = drop.url
    @last_drop = drop
    drop.url
  else
    error "Error uploading file #{file}. Does not exists or is not a file."
  end
end

#upload_from_clipboardObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/gloudapp.rb', line 115

def upload_from_clipboard
  # current cliboard context might not be the same as shown
  # on popup menu creation...
  with_clipboard_text do |text|
    if !text.nil?
      puts "Uploading file from clipboard..."
      upload_file(text)
    end
  end
end

#upload_via_chooserObject



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
# File 'lib/gloudapp.rb', line 150

def upload_via_chooser
  file_dlg = Gtk::FileChooserDialog.new(
    "Upload File", 
    nil, 
    Gtk::FileChooser::ACTION_OPEN, 
    nil,
    [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
    ["Upload", Gtk::Dialog::RESPONSE_ACCEPT])
  
  if file_dlg.run == Gtk::Dialog::RESPONSE_ACCEPT
    file = GLib.filename_to_utf8(file_dlg.filename)
    file_dlg.destroy

    # timeout to close file chooser before blocking gtk thread
    Gtk.timeout_add 50 do
      if upload_file(file)
        @tray.icon = Icon.finish
      end
      false
    end
    false
  else
    file_dlg.destroy
    @tray.icon = Icon.normal
    false
  end
end

#with_clipboard_textObject



140
141
142
143
144
# File 'lib/gloudapp.rb', line 140

def with_clipboard_text
  Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD).request_text do |clipboard, text|
    yield text
  end
end