File: AddFileDialog.vala

package info (click to toggle)
steadyflow 0.2.0-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,384 kB
  • ctags: 57
  • sloc: perl: 1,186; xml: 46; python: 44; makefile: 5
file content (172 lines) | stat: -rw-r--r-- 4,580 bytes parent folder | download | duplicates (2)
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
172
/*
    AddFileDialog.vala
    Copyright (C) 2010 Maia Kozheva <sikon@ubuntu.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using Gtk;
using Steadyflow.Core;
using Steadyflow.UI;

namespace Steadyflow {

public class AddFileDialog : GtkBuilderDialog {
	private Widget custom_command_area;
	private Entry url_entry;
	private Entry local_name;
	private Entry custom_command;
	private CheckButton append_filename;
	private FileChooserButton save_to;
	private ComboBox finished_action;

	public AddFileDialog (string url) {
		base ("AddFileDialog", null, false);
		
		set_title (_("Add File for Download"));
		set_resizable (true);
		set_border_width (5);
		
		Box vbox = (Box) get_content_area ();
		vbox.set_border_width (5);
		vbox.set_spacing (8);
		
		url_entry = (Entry) get_object ("url_entry");
		local_name = (Entry) get_object ("local_name");
		save_to = (FileChooserButton) get_object ("save_to");
		save_to.set_current_folder (Services.get_default_download_folder());
		
		finished_action = (ComboBox) get_object ("finished_action");
		CellRendererText renderer = new CellRendererText ();
		finished_action.pack_start (renderer, true);
		finished_action.add_attribute (renderer, "text", 0);
		finished_action.active = Services.settings.get_enum ("default-post-download-action");
		
		custom_command_area = (Widget) get_object ("custom_command_area");
		custom_command = (Entry) get_object ("custom_command");
		append_filename = (CheckButton) get_object ("append_filename");
		
		UIUtil.install_clear_handler (url_entry);
		UIUtil.install_clear_handler (local_name);
		
		response.connect (on_dialog_response);
		
		add_button (Stock.CANCEL, ResponseType.CANCEL);
		add_button (Stock.ADD, ResponseType.OK);
		set_default_response (ResponseType.OK);
		autoconnect ();

		url_entry.text = url;
		
		if (url == "") {
			Clipboard.get (Gdk.SELECTION_CLIPBOARD).request_text ((clipboard, cbtext) => {
				string text = cbtext.strip ();
				
				if (Util.is_valid_url (text)) {
					url_entry.text = text;
				}
			});
		}
	}
	
	public string get_url () {
		return url_entry.text;
	}
	
	public string get_local_name () {
		return local_name.text;
	}
	
	public string? get_save_path () {
		if (local_name.text == "") {
			return null;
		}
		
		try {
			return save_to.get_current_folder_file ().get_child_for_display_name (local_name.text).get_path ();
		} catch (Error e) {
			return null;
		}
	}
	
	public IDownloadFile.FinishAction get_finish_action () {
		return (IDownloadFile.FinishAction) finished_action.active;
	}
	
	public string? get_finish_command () {
		if (get_finish_action () != IDownloadFile.FinishAction.RUN_COMMAND) {
			return null;
		}
		
		string command = custom_command.text.strip ();
		
		if (command == "") {
			return null;
		}
		
		if (append_filename.active) {
			string? save_path = get_save_path ();
			
			if (save_path != null) {
				command += " " + Shell.quote (save_path);
			}
		}
		
		return command;
	}
	
	[CCode (instance_pos = -1)]
	protected void on_url_changed (Entry sender) {
		// Trim whitespace
		string text = sender.text.strip ();
		
		if (sender.text != text) {
			sender.text = text;
		}
		
		File file = File.new_for_uri (text);
		string? basename = file.get_basename ();
		
		if (basename != null) {
			local_name.text = basename;
		}
	}

	[CCode (instance_pos = -1)]
	protected void on_finished_action_changed (ComboBox sender) {
		if (get_finish_action () == IDownloadFile.FinishAction.RUN_COMMAND) {
			custom_command_area.show ();
		} else if (custom_command_area.visible) {
			Allocation alloc;
			custom_command_area.get_allocation (out alloc);
		
			int height = alloc.height;
			custom_command_area.hide ();
			get_allocation (out alloc);
			resize (alloc.width, alloc.height - height - 6);
		}
	}
	
	private void on_dialog_response (int response_id) {
		if (response_id == (int) ResponseType.OK) {
			file_add_request ();
		} else {
			destroy ();
		}
	}
	
	public signal void file_add_request ();
}

}