~elementary-apps/pantheon-files/trunk

« back to all changes in this revision

Viewing changes to filechooser-module/LocationBarChooser.vala

Merge trunk to r2319

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2
 
/*-
3
 
 * Copyright (c) 2015-2016 elementary LLC (http://launchpad.net/elementary)
4
 
 *
5
 
 * This library is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU Library General Public
7
 
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
9
 
 *
10
 
 * This library is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
 * Library General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU Library General Public
16
 
 * License along with this library; if not, write to the
17
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
 
 * Boston, MA 02111-1307, USA.
19
 
 *
20
 
 * Authored by: Corentin Noël <tintou@mailoo.org>
21
 
 */
22
 
 
23
 
namespace Marlin.View.Chrome {
24
 
    public class LocationBar : Gtk.Box {
25
 
        public Breadcrumbs bread;
26
 
 
27
 
        private string _path;
28
 
        public new string path {
29
 
            set {
30
 
                var new_path = GLib.Uri.unescape_string (value);
31
 
                if (new_path != null) {
32
 
                    _path = new_path;
33
 
 
34
 
                    if (!bread.is_focus) {
35
 
                        bread.text = "";
36
 
                        bread.change_breadcrumbs (new_path);
37
 
                    }
38
 
                } else
39
 
                    warning ("Tried to set null path\n");
40
 
            }
41
 
 
42
 
            get {
43
 
                return _path;
44
 
            }
45
 
        }
46
 
 
47
 
        public new signal void activate (GLib.File file);
48
 
        public signal void escape ();
49
 
        public signal void change_to_file (string filename);
50
 
 
51
 
        public override void get_preferred_width (out int minimum_width, out int natural_width) {
52
 
            minimum_width = -1;
53
 
            natural_width = 3000;
54
 
        }
55
 
 
56
 
        public LocationBar (Gtk.Widget parent) {
57
 
            bread = new Breadcrumbs ();
58
 
            bread.escape.connect (() => { escape (); });
59
 
            bread.activate_alternate.connect ((file) => {
60
 
                path = "file://" + file.get_path ();
61
 
                change_to_file (file.get_path ());            
62
 
            });
63
 
            
64
 
            bread.path_changed.connect ((file) => {
65
 
                path = "file://" + file.get_path ();
66
 
                change_to_file (file.get_path ());
67
 
 
68
 
                parent.grab_focus ();
69
 
            });
70
 
            
71
 
            margin_top = 4;
72
 
            margin_bottom = 4;
73
 
            margin_left = 3;
74
 
            pack_start (bread, true, true, 0);
75
 
        }
76
 
    }
77
 
 
78
 
    public class Breadcrumbs : BasePathBar {
79
 
        Gtk.Menu menu;
80
 
 
81
 
        private bool drop_data_ready = false;
82
 
        private bool drop_occurred = false;
83
 
        private GLib.List<GLib.File> drop_file_list = null;
84
 
 
85
 
        Gdk.DragAction current_suggested_action = 0;
86
 
        Gdk.DragAction current_actions = 0;
87
 
 
88
 
        public Breadcrumbs ()
89
 
        {
90
 
            /* The string split of the path url is kinda too basic,
91
 
             * we should use the GFile to split our uris and determine the protocol (if any) with g_uri_parse_scheme or g_file_get_uri_scheme */
92
 
            add_icon ({ "afp://", Marlin.ICON_FOLDER_REMOTE_SYMBOLIC, true, null, null, null, true, Marlin.PROTOCOL_NAME_AFP});
93
 
            add_icon ({ "dav://", Marlin.ICON_FOLDER_REMOTE_SYMBOLIC, true, null, null, null, true, Marlin.PROTOCOL_NAME_DAV});
94
 
            add_icon ({ "davs://", Marlin.ICON_FOLDER_REMOTE_SYMBOLIC, true, null, null, null, true,Marlin.PROTOCOL_NAME_DAVS});
95
 
            add_icon ({ "ftp://", Marlin.ICON_FOLDER_REMOTE_SYMBOLIC, true, null, null, null, true, Marlin.PROTOCOL_NAME_FTP});
96
 
            add_icon ({ "network://", Marlin.ICON_FOLDER_REMOTE_SYMBOLIC, true, null, null, null, true, Marlin.PROTOCOL_NAME_NETWORK});
97
 
            add_icon ({ "sftp://", Marlin.ICON_FOLDER_REMOTE_SYMBOLIC, true, null, null, null, true, Marlin.PROTOCOL_NAME_SFTP});
98
 
            add_icon ({ "smb://", Marlin.ICON_FOLDER_REMOTE_SYMBOLIC, true, null, null, null, true,Marlin.PROTOCOL_NAME_SMB});
99
 
            add_icon ({ "trash://", Marlin.ICON_TRASH_SYMBOLIC, true, null, null, null, true, Marlin.PROTOCOL_NAME_TRASH});
100
 
 
101
 
            string dir;
102
 
            dir = Environment.get_user_special_dir (UserDirectory.MUSIC);
103
 
            if (dir.contains ("/")) {
104
 
                IconDirectory icon = {dir, Marlin.ICON_FOLDER_MUSIC_SYMBOLIC, false, null, null, dir.split ("/"), false, null};
105
 
                icon.exploded[0] = "/";
106
 
                add_icon (icon);
107
 
            }
108
 
 
109
 
 
110
 
            dir = Environment.get_user_special_dir (UserDirectory.PICTURES);
111
 
            if (dir.contains ("/")) {
112
 
                IconDirectory icon = {dir, Marlin.ICON_FOLDER_PICTURES_SYMBOLIC, false, null, null, dir.split ("/"), false, null};
113
 
                icon.exploded[0] = "/";
114
 
                add_icon (icon);
115
 
            }
116
 
 
117
 
 
118
 
            dir = Environment.get_user_special_dir (UserDirectory.VIDEOS);
119
 
            if (dir.contains ("/")) {
120
 
                IconDirectory icon = {dir, Marlin.ICON_FOLDER_VIDEOS_SYMBOLIC, false, null, null, dir.split ("/"), false, null};
121
 
                icon.exploded[0] = "/";
122
 
                add_icon (icon);
123
 
            }
124
 
 
125
 
            dir = Environment.get_user_special_dir (UserDirectory.DOWNLOAD);
126
 
            if (dir.contains ("/")) {
127
 
                IconDirectory icon = {dir, Marlin.ICON_FOLDER_DOWNLOADS_SYMBOLIC, false, null, null, dir.split ("/"), false, null};
128
 
                icon.exploded[0] = "/";
129
 
                add_icon (icon);
130
 
            }
131
 
 
132
 
            dir = Environment.get_user_special_dir (UserDirectory.DOCUMENTS);
133
 
            if (dir.contains ("/")) {
134
 
                IconDirectory icon = {dir, Marlin.ICON_FOLDER_DOCUMENTS_SYMBOLIC, false, null, null, dir.split ("/"), false, null};
135
 
                icon.exploded[0] = "/";
136
 
                add_icon (icon);
137
 
            }
138
 
 
139
 
            dir = Environment.get_user_special_dir (UserDirectory.TEMPLATES);
140
 
            if (dir.contains ("/")) {
141
 
                IconDirectory icon = {dir, Marlin.ICON_FOLDER_TEMPLATES_SYMBOLIC, false, null, null, dir.split ("/"), false, null};
142
 
                icon.exploded[0] = "/";
143
 
                add_icon (icon);
144
 
            }
145
 
 
146
 
            dir = Environment.get_home_dir ();
147
 
            if (dir.contains ("/")) {
148
 
                IconDirectory icon = {dir, Marlin.ICON_GO_HOME_SYMBOLIC, false, null, null, dir.split ("/"), true, null};
149
 
                icon.exploded[0] = "/";
150
 
                add_icon (icon);
151
 
            }
152
 
 
153
 
 
154
 
            dir = "/media";
155
 
            if (dir.contains ("/")) {
156
 
                IconDirectory icon = {dir, Marlin.ICON_FILESYSTEM_SYMBOLIC, false, null, null, dir.split ("/"), true, null};
157
 
                icon.exploded[0] = "/";
158
 
                add_icon (icon);
159
 
            }
160
 
 
161
 
            IconDirectory icon = {"/", Marlin.ICON_FILESYSTEM_SYMBOLIC, false, null, null, null, false, null};
162
 
            icon.exploded = {"/"};
163
 
            add_icon (icon);
164
 
 
165
 
            menu = new Gtk.Menu ();
166
 
            menu.show_all ();
167
 
        }
168
 
 
169
 
        protected override void load_right_click_menu (double x, double y) {
170
 
 
171
 
        }
172
 
 
173
 
        protected override bool on_drag_motion (Gdk.DragContext context, int x, int y, uint time) {
174
 
            Gtk.drag_unhighlight (this);
175
 
 
176
 
            foreach (BreadcrumbsElement element in elements)
177
 
                element.pressed = false;
178
 
 
179
 
            var el = get_element_from_coordinates (x, y);
180
 
 
181
 
            if (el != null)
182
 
                el.pressed = true;
183
 
            else
184
 
                /* No action taken on drop */
185
 
                Gdk.drag_status (context, 0, time);
186
 
 
187
 
            queue_draw ();
188
 
 
189
 
            return false;
190
 
        }
191
 
 
192
 
        protected override bool on_drag_drop (Gdk.DragContext context,
193
 
                                   int x,
194
 
                                   int y,
195
 
                                   uint timestamp) {
196
 
            Gtk.TargetList list = null;
197
 
            bool ok_to_drop = false;
198
 
 
199
 
            Gdk.Atom target = Gtk.drag_dest_find_target  (this, context, list);
200
 
 
201
 
            ok_to_drop = (target != Gdk.Atom.NONE);
202
 
 
203
 
            if (ok_to_drop) {
204
 
                drop_occurred = true;
205
 
                Gtk.drag_get_data (this, context, target, timestamp);
206
 
            }
207
 
 
208
 
            return ok_to_drop;
209
 
        }
210
 
 
211
 
        protected override void on_drag_data_received (Gdk.DragContext context,
212
 
                                            int x,
213
 
                                            int y,
214
 
                                            Gtk.SelectionData selection_data,
215
 
                                            uint info,
216
 
                                            uint timestamp) {
217
 
            bool success = false;
218
 
 
219
 
            if (!drop_data_ready) {
220
 
                drop_file_list = null;
221
 
                foreach (var uri in selection_data.get_uris ()) {
222
 
                    debug ("Path to move: %s\n", uri);
223
 
                    drop_file_list.append (File.new_for_uri (uri));
224
 
                    drop_data_ready = true;
225
 
                }
226
 
            }
227
 
 
228
 
            if (drop_data_ready && drop_occurred && info == TargetType.TEXT_URI_LIST) {
229
 
                drop_occurred = false;
230
 
                current_actions = 0;
231
 
                current_suggested_action = 0;
232
 
 
233
 
                Gtk.drag_finish (context, success, false, timestamp);
234
 
                on_drag_leave (context, timestamp);
235
 
            }
236
 
        }
237
 
 
238
 
        protected override void on_drag_leave (Gdk.DragContext drag_context, uint time) {
239
 
            foreach (BreadcrumbsElement element in elements) {
240
 
                if (element.pressed) {
241
 
                    element.pressed = false;
242
 
                    break;
243
 
                }
244
 
            }
245
 
 
246
 
            drop_occurred = false;
247
 
            drop_data_ready = false;
248
 
            drop_file_list = null;
249
 
 
250
 
            queue_draw ();
251
 
        }
252
 
    }
253
 
}