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

def initialize
	@client = CloudApp::Client.new

	login!

	create_tray
end

Instance Method Details

#check_clipboard(item) ⇒ Object



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

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



148
149
150
151
152
153
154
155
156
# File 'lib/gloudapp.rb', line 148

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



168
169
170
# File 'lib/gloudapp.rb', line 168

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

#copy_last_drop_urlObject



158
159
160
# File 'lib/gloudapp.rb', line 158

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

#create_trayObject



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

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...") { 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)


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

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

#error(message) ⇒ Object



227
228
229
230
231
232
233
234
235
# File 'lib/gloudapp.rb', line 227

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

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

#load_credentials(name) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/gloudapp.rb', line 71

def load_credentials(name)
	config_file = File.join(ENV['HOME'], name)
	if File.exists?(config_file)
		return YAML.load_file(config_file)
	end
	nil
end

#login!Object



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

def login!
	if ARGV.length == 2
		# assume that's username and password in ARGV
		@credentials = {:username => ARGV[0], :password => ARGV[1]}
		@client.authenticate(@credentials[:username], @credentials[:password])
		return if credentials_valid?
		ErrorDialog.run!("GloudApp - Error", "Authentication failed!")
		exit 1
	end

	default_remember = false

	@credentials = load_credentials('.gloudapp')
	if not @credentials.nil?
		@client.authenticate(@credentials[:username], @credentials[:password])
		return if credentials_valid?
		# make the login dialog remember the new credentials by default
		# since the old ones in .gloudapp are wrong
		default_remember = true
	else
		@credentials = load_credentials('.cloudapp-cli')
		if not @credentials.nil?
			@client.authenticate(@credentials[:username], @credentials[:password])
			return if credentials_valid?
		end
	end

	@credentials = request_credentials(default_remember)
	if not @credentials.nil?
		@client.authenticate(@credentials[:username], @credentials[:password])
		return if credentials_valid?
	end
	ErrorDialog.run!("GloudApp - Error", "Authentication failed!")
	exit 1
end

#request_credentials(default_remember) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gloudapp.rb', line 79

def request_credentials(default_remember)
	 = LoginDialog.new(default_remember)
	case .run
	when Gtk::Dialog::RESPONSE_ACCEPT
		creds = {:username => ..text, :password => .password.text}
		if .remember.active?
			File.open(File.join(ENV['HOME'], '.gloudapp'), 'w+') do |f| YAML.dump(creds, f) end
		end
		.destroy
		return creds
	when Gtk::Dialog::RESPONSE_REJECT
		.destroy
		return nil
	end
end

#run!Object



21
22
23
24
# File 'lib/gloudapp.rb', line 21

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

#take_screenshotObject



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/gloudapp.rb', line 214

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



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/gloudapp.rb', line 200

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



137
138
139
140
141
142
143
144
145
146
# File 'lib/gloudapp.rb', line 137

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



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

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_path
			end
			false
		end
		false
	else
		file_dlg.destroy
		@tray.icon = Icon.normal_path
		false
	end
end

#with_clipboard_textObject



162
163
164
165
166
# File 'lib/gloudapp.rb', line 162

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