~vikoadi/arrive/0.1

« back to all changes in this revision

Viewing changes to src/Widgets/DownloadingList.vala

  • Committer: Viko Adi Rahmawan
  • Date: 2013-05-10 12:18:17 UTC
  • Revision ID: vikoadi@gmail.com-20130510121817-xic6cs3t2gb3wi0m
code tidieing

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
public class Arrive.Widgets.DownloadingList : Object {
2
 
    private static int REFRESH_TIME=1000;
3
 
    public Gtk.ScrolledWindow widget;
4
 
    private Gtk.ListStore list_store;
5
 
    private Gtk.TreeView tree_view;
6
 
    
7
 
    public DownloadingList () {
8
 
        /*Gtk.ListStore*/ list_store = new Gtk.ListStore (1, typeof(Arrive.Model.DownloadItem));
9
 
        /*Gtk.TreeView*/ tree_view = new Gtk.TreeView.with_model (list_store);
10
 
        tree_view.set_headers_visible (false);
11
 
        tree_view.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE);
12
 
        widget = new Gtk.ScrolledWindow(null, null);
13
 
        widget.add(tree_view);
14
 
 
15
 
        tree_view.button_press_event.connect((event)=>{
16
 
            if(event.button==3){                        
17
 
                show_popup_menu(event);
18
 
            }
19
 
            return false;
20
 
        });
21
 
                
22
 
        Arrive.App.aria2.download_list.list_changed.connect(()=>{
23
 
            populate_list_store();
24
 
        });
25
 
        
26
 
        var cell_renderer = new Arrive.Widgets.DownloadCellRenderer ();
27
 
        tree_view.insert_column_with_attributes (-1, "column", cell_renderer, "file", 0);
28
 
        
29
 
        var refresh_timer = new TimeoutSource(REFRESH_TIME);
30
 
        refresh_timer.set_callback(()=>{
31
 
            tree_view.queue_draw();
 
1
namespace Arrive.Widgets {
 
2
    public class DownloadingList : Object {
 
3
        private static int REFRESH_TIME=1000;
 
4
        public Gtk.ScrolledWindow widget;
 
5
        private Gtk.ListStore list_store;
 
6
        private Gtk.TreeView tree_view;
 
7
 
 
8
        public DownloadingList () {
 
9
            /*Gtk.ListStore*/ list_store = new Gtk.ListStore (1, typeof(Arrive.Model.DownloadItem));
 
10
            /*Gtk.TreeView*/ tree_view = new Gtk.TreeView.with_model (list_store);
 
11
            tree_view.set_headers_visible (false);
 
12
            tree_view.get_selection ().set_mode (Gtk.SelectionMode.MULTIPLE);
 
13
            widget = new Gtk.ScrolledWindow (null, null);
 
14
            widget.add (tree_view);
 
15
 
 
16
            tree_view.button_press_event.connect ((event)=>{
 
17
                                                      if(event.button == 3)
 
18
                                                          show_popup_menu (event);
 
19
                                                      return false;
 
20
                                                  });
 
21
 
 
22
            Arrive.App.aria2.download_list.list_changed.connect (()=>{
 
23
                                                                     populate_list_store ();
 
24
                                                                 });
 
25
 
 
26
            var cell_renderer = new DownloadCellRenderer ();
 
27
            tree_view.insert_column_with_attributes (-1, "column", cell_renderer, "file", 0);
 
28
 
 
29
            var refresh_timer = new TimeoutSource (REFRESH_TIME);
 
30
            refresh_timer.set_callback (()=>{
 
31
                                            tree_view.queue_draw ();
 
32
                                            return true;
 
33
                                        });
 
34
            refresh_timer.attach (null);
 
35
 
 
36
            populate_list_store ();
 
37
            debug ("DownloadingList created");
 
38
        }
 
39
        private void populate_list_store(){
 
40
            list_store.clear ();
 
41
            debug ("list lenght %u", Arrive.App.aria2.download_list._list.length ());
 
42
            foreach(Arrive.Model.DownloadItem file in Arrive.App.aria2.download_list._list) {
 
43
                Gtk.TreeIter iter;
 
44
                list_store.append (out iter);
 
45
                list_store.set (iter, 0, file);
 
46
            }
 
47
        }
 
48
        private void show_popup_menu(Gdk.EventButton event){
 
49
            List<Arrive.Model.DownloadItem> selected_files;
 
50
            selected_files=get_selected_files ();
 
51
            var menu = new Gtk.Menu ();
 
52
            var start = new Gtk.MenuItem.with_label (_("Continue"));
 
53
            var pause = new Gtk.MenuItem.with_label (_("Pause"));
 
54
            var cancel = new Gtk.MenuItem.with_label (_("Cancel"));
 
55
            var properties = new Gtk.MenuItem.with_label (_("Properties"));
 
56
            //TODO:implement right click event
 
57
            start.activate.connect (()=>{
 
58
                                        foreach(Arrive.Model.DownloadItem d_item in selected_files) {
 
59
                                            if(d_item.status == "paused") d_item.unpause ();
 
60
                                            if(d_item.status == "") d_item.start (null);
 
61
                                            if(d_item.status == "stopped") d_item.start (null);
 
62
                                        }
 
63
                                    });
 
64
            pause.activate.connect (()=>{
 
65
                                        foreach(Arrive.Model.DownloadItem d_item in selected_files)
 
66
                                            d_item.pause ();
 
67
                                    });
 
68
            cancel.activate.connect (()=>{
 
69
                                         foreach(Arrive.Model.DownloadItem d_item in selected_files)
 
70
                                             d_item.remove ();
 
71
                                     });
 
72
            //~         properties.connect();
 
73
 
 
74
            if(allow_start (selected_files)) menu.add (start);
 
75
            if(allow_pause (selected_files)) menu.add (pause);
 
76
            if(allow_cancel (selected_files)) menu.add (cancel);
 
77
            if(allow_properties (selected_files)) menu.add (properties);
 
78
 
 
79
            menu.attach_to_widget (tree_view, null);
 
80
            menu.show_all ();
 
81
            menu.popup (null, null, null, event.button, event.time);
 
82
        }
 
83
        private bool allow_start(List<Arrive.Model.DownloadItem> selected_files){
 
84
            bool allow=false;
 
85
            foreach(Arrive.Model.DownloadItem d_item in selected_files) {
 
86
                if(d_item.status == "paused") allow=true;
 
87
                if(d_item.status == "") allow=true;
 
88
            }
 
89
            return allow;
 
90
        }
 
91
        private bool allow_pause(List<Arrive.Model.DownloadItem> selected_files){
 
92
            bool allow=false;
 
93
            foreach(Arrive.Model.DownloadItem d_item in selected_files)
 
94
                if(d_item.status == "active") allow=true;
 
95
            return allow;
 
96
        }
 
97
        private bool allow_cancel(List<Arrive.Model.DownloadItem> selected_files){
 
98
            bool allow=false;
 
99
            foreach(Arrive.Model.DownloadItem d_item in selected_files)
 
100
                if(d_item.status == "active"||d_item.status == "paused") allow=true;
 
101
            return allow;
 
102
        }
 
103
        private bool allow_properties(List<Arrive.Model.DownloadItem> selected_files){
32
104
            return true;
33
 
        });
34
 
        refresh_timer.attach(null);
35
 
        
36
 
        populate_list_store();
37
 
        debug("DownloadingList created");
38
 
    }
39
 
    private void populate_list_store(){
40
 
        list_store.clear();
41
 
        debug("list lenght %u",Arrive.App.aria2.download_list._list.length());
42
 
        foreach(Arrive.Model.DownloadItem file in Arrive.App.aria2.download_list._list){
43
 
            Gtk.TreeIter iter;
44
 
            list_store.append(out iter);
45
 
            list_store.set(iter,0,file);
46
 
        }
47
 
    }
48
 
    private void show_popup_menu(Gdk.EventButton event){
49
 
        List<Arrive.Model.DownloadItem> selected_files;
50
 
        selected_files=get_selected_files();
51
 
        var menu = new Gtk.Menu();
52
 
        var start = new Gtk.MenuItem.with_label(_("Continue"));
53
 
        var pause = new Gtk.MenuItem.with_label(_("Pause"));
54
 
        var cancel = new Gtk.MenuItem.with_label(_("Cancel"));
55
 
        var properties = new Gtk.MenuItem.with_label(_("Properties"));
56
 
        //TODO:implement right click event
57
 
        start.activate.connect(()=>{
58
 
            foreach(Arrive.Model.DownloadItem d_item in selected_files){
59
 
                if(d_item.status=="paused")d_item.unpause();
60
 
                if(d_item.status=="")d_item.start(null);
61
 
                if(d_item.status=="stopped")d_item.start(null);
62
 
            }
63
 
        });
64
 
        pause.activate.connect(()=>{
65
 
            foreach(Arrive.Model.DownloadItem d_item in selected_files){
66
 
                d_item.pause();
67
 
            }
68
 
        });
69
 
        cancel.activate.connect(()=>{
70
 
            foreach(Arrive.Model.DownloadItem d_item in selected_files){
71
 
                d_item.remove();
72
 
            }
73
 
        });
74
 
//~         properties.connect();
75
 
        
76
 
        if(allow_start(selected_files))menu.add(start);
77
 
        if(allow_pause(selected_files))menu.add(pause);
78
 
        if(allow_cancel(selected_files))menu.add(cancel);
79
 
        if(allow_properties(selected_files))menu.add(properties);
80
 
        
81
 
        menu.attach_to_widget(tree_view,null);
82
 
        menu.show_all();
83
 
        menu.popup(null,null,null,event.button,event.time);
84
 
    }
85
 
    private bool allow_start(List<Arrive.Model.DownloadItem> selected_files){
86
 
        bool allow=false;
87
 
        foreach(Arrive.Model.DownloadItem d_item in selected_files){
88
 
            if(d_item.status=="paused")allow=true;
89
 
            if(d_item.status=="")allow=true;
90
 
        }
91
 
        return allow;
92
 
    }
93
 
    private bool allow_pause(List<Arrive.Model.DownloadItem> selected_files){
94
 
        bool allow=false;
95
 
        foreach(Arrive.Model.DownloadItem d_item in selected_files){
96
 
            if(d_item.status=="active")allow=true;
97
 
        }
98
 
        return allow;
99
 
    }
100
 
    private bool allow_cancel(List<Arrive.Model.DownloadItem> selected_files){
101
 
        bool allow=false;
102
 
        foreach(Arrive.Model.DownloadItem d_item in selected_files){
103
 
            if(d_item.status=="active"||d_item.status=="paused")allow=true;
104
 
        }
105
 
        return allow;
106
 
    }
107
 
    private bool allow_properties(List<Arrive.Model.DownloadItem> selected_files){
108
 
        return true;
109
 
    }
110
 
    private List<Arrive.Model.DownloadItem> get_selected_files(){
111
 
        var list = new List<Arrive.Model.DownloadItem>();
112
 
        Gtk.TreeIter selection_iter;
113
 
        Gtk.TreeSelection selection=tree_view.get_selection();
114
 
        GLib.List<Gtk.TreePath> d_items = selection.get_selected_rows(null);
115
 
        Gtk.TreeModel model = tree_view.get_model();
116
 
        foreach(Gtk.TreePath selection_item in d_items){
117
 
           /*Arrive.Model.DownloadItem*/Value d_item;
118
 
           model.get_iter (out selection_iter, selection_item);
119
 
           model.get_value(selection_iter,0, out d_item);
120
 
           if(d_item.holds(typeof(Arrive.Model.DownloadItem))){
121
 
                list.append((Arrive.Model.DownloadItem)d_item);
122
 
            } else {
123
 
                debug("value arent download item");
124
 
            }
125
 
        }
126
 
        
127
 
        return list;
 
105
        }
 
106
        private List<Arrive.Model.DownloadItem> get_selected_files(){
 
107
            var list = new List<Arrive.Model.DownloadItem>();
 
108
            Gtk.TreeIter selection_iter;
 
109
            Gtk.TreeSelection selection=tree_view.get_selection ();
 
110
            GLib.List<Gtk.TreePath> d_items = selection.get_selected_rows (null);
 
111
            Gtk.TreeModel model = tree_view.get_model ();
 
112
            foreach(Gtk.TreePath selection_item in d_items) {
 
113
                /*Arrive.Model.DownloadItem*/ Value d_item;
 
114
                model.get_iter (out selection_iter, selection_item);
 
115
                model.get_value (selection_iter, 0, out d_item);
 
116
                if(d_item.holds (typeof(Arrive.Model.DownloadItem)))
 
117
                    list.append ((Arrive.Model.DownloadItem)d_item);
 
118
                else
 
119
                    debug ("value arent download item");
 
120
            }
 
121
 
 
122
            return list;
 
123
        }
128
124
    }
129
125
}