~teejee2008/indicator-diskman/trunk

« back to all changes in this revision

Viewing changes to src/DiskIndicator.vala

  • Committer: Tony George
  • Date: 2016-10-19 15:40:05 UTC
  • Revision ID: tony.george.kol@gmail.com-20161019154005-4yl47s0pszajghnn
Add menu for disk usage

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 
15
15
using AppIndicator;
16
16
 
17
 
public class DiskIndicator
18
 
{
 
17
public class DiskIndicator: GLib.Object{
 
18
        
19
19
        protected Gee.ArrayList<Device> device_list;
20
20
    protected Indicator indicator;
21
21
    protected string icon;
49
49
                });
50
50
 
51
51
                item.activate();
52
 
                
 
52
 
53
53
                // eject -------------------------------------
54
54
 
55
55
        /*
80
80
                });
81
81
 
82
82
                item.activate();
83
 
        
 
83
 
84
84
                // unmount -------------------------------------
85
85
                
86
86
        item = new Gtk.MenuItem.with_label(_("Unmount"));
109
109
                });
110
110
 
111
111
                item.activate();
112
 
                
 
112
 
113
113
                // unlock -------------------------------------
114
114
                
115
115
        item = new Gtk.MenuItem.with_label(_("Unlock"));
122
122
                });
123
123
 
124
124
                item.activate();
125
 
                
 
125
 
 
126
                // usage -------------------------------------
 
127
        
 
128
                separator = new Gtk.SeparatorMenuItem ();
 
129
                menu.add (separator);
 
130
                
 
131
        item = new Gtk.MenuItem.with_label("Usage");
 
132
        menu.append(item);
 
133
                var item_usage = item;
 
134
                
 
135
        item.activate.connect(()=>{
 
136
                        var submenu = get_menu("usage");
 
137
                        item_usage.set_submenu(submenu);
 
138
                });
 
139
 
 
140
                item.activate();
 
141
 
126
142
                // about -------------------------------------
127
143
                
128
144
        separator = new Gtk.SeparatorMenuItem ();
161
177
        private void refresh_device_list_if_stale(){
162
178
                var period = (new DateTime.now_local()).add_seconds(-10);
163
179
                if ((device_list == null) || (last_refresh_date == null) || (last_refresh_date.compare(period) < 0)){
164
 
                        device_list = Device.get_block_devices_using_lsblk();
 
180
                        device_list = Device.get_filesystems();
165
181
                        last_refresh_date = new DateTime.now_local();
166
182
                }
167
183
        }
168
184
 
169
185
        private void refresh_device_list(){
170
 
                device_list = Device.get_block_devices_using_lsblk();
 
186
                device_list = Device.get_filesystems();
171
187
                last_refresh_date = new DateTime.now_local();
172
188
        }
173
189
        
192
208
                        
193
209
                        switch(action){
194
210
                        case "open":
 
211
                        case "usage":
195
212
                                bool show = (dev.type == "disk") || (dev.type == "loop")  || !dev.is_encrypted_partition() || !dev.has_children(); 
196
213
                                if (!show){
197
214
                                        continue;
232
249
                        }
233
250
                        
234
251
                        Gtk.Image icon = null;
 
252
 
 
253
                        //if (action != "usage"){
 
254
                                if ((dev.type == "crypt") && (dev.pkname.length > 0)){
 
255
                                        icon = get_shared_icon("","unlocked.png",16);
 
256
                                }
 
257
                                else if (dev.fstype.contains("luks")){
 
258
                                        icon = get_shared_icon("","locked.png",16);
 
259
                                }
 
260
                                else if (dev.fstype.contains("iso9660") || (dev.type == "loop")){
 
261
                                        icon = get_shared_icon("media-cdrom","media-cdrom.png",16);
 
262
                                }
 
263
                                else{
 
264
                                        icon = get_shared_icon("","drive-harddisk.svg",16);
 
265
                                }
 
266
                        //}
 
267
 
 
268
                        var name = "";
 
269
 
 
270
                        switch(action){
 
271
                        case "usage":
 
272
                                if ((dev.type == "disk") || ((dev.type == "loop") && dev.has_children())){
 
273
                                        name += "%s".printf(dev.description_simple());
 
274
                                        break;
 
275
                                }
235
276
                        
236
 
                        if ((dev.type == "crypt") && (dev.pkname.length > 0)){
237
 
                                icon = get_shared_icon("","unlocked.png",16);
238
 
                        }
239
 
                        else if (dev.fstype.contains("luks")){
240
 
                                icon = get_shared_icon("","locked.png",16);
241
 
                        }
242
 
                        else if (dev.fstype.contains("iso9660") || (dev.type == "loop")){
243
 
                                icon = get_shared_icon("media-cdrom","media-cdrom.png",16);
244
 
                        }
245
 
                        else{
246
 
                                icon = get_shared_icon("","drive-harddisk.svg",16);
247
 
                        }
248
 
 
249
 
                        var name = dev.description_simple();
 
277
                                
 
278
                                name += "%s".printf(dev.description_usage());
 
279
 
 
280
                                if ((dev.used_bytes > 0) && (dev.size_bytes > 0)){
 
281
                                        double usage_percent = (dev.used_bytes * 10.0) / dev.size_bytes;
 
282
                                        int used_count = (int) usage_percent;
 
283
                                        int free_count = 10 - used_count;
 
284
 
 
285
                                        name += "  ";
 
286
                                        for(int j = 0; j < used_count; j++){
 
287
                                                name += "▮";
 
288
                                        }
 
289
                                        for(int j = 0; j < free_count; j++){
 
290
                                                name += "▯";
 
291
                                        }
 
292
                                }
 
293
                                else{
 
294
                                        name += "  ";
 
295
                                        for(int j = 0; j < 10; j++){
 
296
                                                //name += "░";░ ▒ ▓ ⬜ ⬛ ▯ ▮
 
297
                                        }
 
298
                                }
 
299
                                
 
300
                                break;
 
301
                                
 
302
                        default:
 
303
                                name += "%s".printf(dev.description_simple());
 
304
                                break;
 
305
                        }
250
306
 
251
307
                        if ((dev.type != "disk") && ((action == "open") || (action == "unmount"))){
252
308
                                if (dev.mount_points.size > 0){
299
355
                                }
300
356
                                break;
301
357
                        case "open":
 
358
                        case "usage":
302
359
                        case "mount":
303
360
                        case "unmount":
304
361
                        case "lock":
539
596
                                        
540
597
                                });
541
598
                                break;
 
599
 
 
600
                        case "usage":
 
601
                        
 
602
                                item.activate.connect(() => {
 
603
                                        open_baobab(dev);
 
604
                                });
 
605
                                
 
606
                                break;
542
607
                        }
543
608
                }
544
609
 
650
715
                return (dev_unlocked_new == null);
651
716
        }
652
717
 
 
718
        private void open_baobab(Device dev){
 
719
 
 
720
                if ((dev.used_bytes > 0) && (dev.mount_points.size > 0)){
 
721
                        string cmd = "baobab '%s'\n".printf(dev.mount_points[0].mount_point);
 
722
                        string std_out, std_err;
 
723
                        exec_script_sync(cmd, out std_out, out std_err, false, true);
 
724
                }
 
725
                
 
726
                /*if ((dev.used_bytes > 0) && (dev.mount_points.size > 0)){
 
727
                        
 
728
                        App.init_daemon();
 
729
 
 
730
                        while (!App.daemon.is_ready){
 
731
                                sleep(100);
 
732
                        }
 
733
 
 
734
                        string cmd = "open_baobab|%s".printf(dev.mount_points[0].mount_point);
 
735
                        App.daemon.send_command(cmd);
 
736
                }*/
 
737
        }
 
738
 
653
739
        public void btn_donate_clicked(){
654
740
                var dialog = new DonationWindow();
655
741
                //dialog.set_transient_for(this);