~chris.foo/bytestag/trunk

« back to all changes in this revision

Viewing changes to src/py3/bytestagui/gtk/controllers/sharedfiles.py

  • Committer: Christopher Foo
  • Date: 2012-09-09 15:38:30 UTC
  • mfrom: (24.1.28 trunk)
  • Revision ID: chris.foo@gmail.com-20120909153830-575fqdbsju8dmg0k
Merges from trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
'''Shared files screen'''
2
 
# This file is part of Bytestag.
3
 
# Copyright © 2012 Christopher Foo <chris.foo@gmail.com>.
4
 
# Licensed under GNU GPLv3. See COPYING.txt for details.
5
 
from bytestagui.abstract.controllers.sharedfiles import (
6
 
    SharedFilesController as BaseSharedFilesController)
7
 
from bytestagui.abstract.views.sharedfiles import (DIRECTORY_HEADER_TEXT,
8
 
    SCAN_PROGRESS_TEXT)
9
 
from bytestagui.gtk.controllers.builder import BuilderController
10
 
from bytestagui.gtk.controllers.dht import DHTClientController
11
 
from gi.repository import Gtk, GLib # @UnresolvedImport
12
 
 
13
 
 
14
 
class SharedFilesController(BaseSharedFilesController):
15
 
    def __init__(self, application):
16
 
        BaseSharedFilesController.__init__(self, application)
17
 
        self._builder = self.application.singletons[BuilderController].builder
18
 
        self._paths_shown = set()
19
 
        self._scan_task = None
20
 
 
21
 
        self.application.singletons[BuilderController].add_signals({
22
 
            'shared_files_add_button_clicked_cb':
23
 
                self._shared_files_add_button_clicked_cb,
24
 
            'shared_files_remove_button_clicked_cb':
25
 
                self._shared_files_remove_button_clicked_cb,
26
 
            'shared_files_scan_button_clicked_cb':
27
 
                self._shared_files_scan_button_clicked_cb,
28
 
            'shared_files_scan_stop_button_clicked_cb':
29
 
                self._shared_files_scan_stop_button_clicked_cb,
30
 
            'shared_files_file_chooser_dialog_close_cb':
31
 
                self._shared_files_file_chooser_dialog_close_cb,
32
 
            'shared_files_file_chooser_dialog_response_cb':
33
 
                self._shared_files_file_chooser_dialog_response_cb,
34
 
            'shared_files_file_chooser_dialog_delete_event_cb':
35
 
                self._shared_files_file_chooser_dialog_delete_event_cb,
36
 
            'shared_files_tree_view_cursor_changed_cb':
37
 
                self._shared_files_tree_view_cursor_changed_cb,
38
 
        })
39
 
 
40
 
        self._disable_scan_ui()
41
 
        self._create_tree_view_columns()
42
 
        self._populate_tree_view()
43
 
 
44
 
    def _create_tree_view_columns(self):
45
 
        builder = self._builder
46
 
        shared_files_tree_view = builder.get_object('shared_files_tree_view')
47
 
 
48
 
        path_cell_renderer = Gtk.CellRendererText()
49
 
        path_column = Gtk.TreeViewColumn(DIRECTORY_HEADER_TEXT,
50
 
            path_cell_renderer, text=0)
51
 
 
52
 
        shared_files_tree_view.append_column(path_column)
53
 
 
54
 
    def _disable_scan_ui(self):
55
 
        self._builder.get_object('shared_files_scan_box').hide()
56
 
        self._builder.get_object('shared_files_scan_button'
57
 
            ).set_sensitive(True)
58
 
        self._builder.get_object('shared_files_scan_spinner').stop()
59
 
 
60
 
        if self._scan_task:
61
 
            assert self._scan_task.is_finished
62
 
 
63
 
        self._scan_task = None
64
 
 
65
 
    def _enable_scan_ui(self):
66
 
        self._builder.get_object('shared_files_scan_box').show_all()
67
 
        self._builder.get_object('shared_files_scan_button'
68
 
            ).set_sensitive(False)
69
 
        self._builder.get_object('shared_files_scan_spinner').start()
70
 
 
71
 
    def _shared_files_add_button_clicked_cb(self, *args):
72
 
        self._builder.get_object('shared_files_file_chooser_dialog').show_all()
73
 
 
74
 
    def _shared_files_remove_button_clicked_cb(self, *args):
75
 
        shared_files_tree_view = self._builder.get_object(
76
 
            'shared_files_tree_view')
77
 
        selection = shared_files_tree_view.get_selection()
78
 
        model, tree_iter = selection.get_selected()
79
 
        path = model[tree_iter][0]
80
 
 
81
 
        self._shared_directories.remove(path)
82
 
        self._paths_shown.remove(path)
83
 
        del model[tree_iter]
84
 
        self._save_shared_files_config()
85
 
 
86
 
    def _shared_files_scan_button_clicked_cb(self, widget, *args):
87
 
        self._enable_scan_ui()
88
 
 
89
 
        self._scan_task = self.application.singletons[
90
 
            DHTClientController].client.shared_files_table.hash_directories()
91
 
 
92
 
        def f(*args):
93
 
            GLib.idle_add(self._disable_scan_ui)
94
 
 
95
 
        self._scan_task.observer.register(f)
96
 
        GLib.timeout_add(200, self._update_scan_progress)
97
 
 
98
 
    def _shared_files_scan_stop_button_clicked_cb(self, *args):
99
 
        def f(*args):
100
 
            GLib.idle_add(self._disable_scan_ui)
101
 
 
102
 
        self._scan_task.observer.register(f)
103
 
        self._scan_task.stop()
104
 
 
105
 
    def _shared_files_file_chooser_dialog_response_cb(self, widget,
106
 
    response_id, *args):
107
 
        widget.hide()
108
 
        if response_id == 1:
109
 
            for path in widget.get_filenames():
110
 
                self._add_directory(path)
111
 
 
112
 
            self._save_shared_files_config()
113
 
 
114
 
    def _shared_files_file_chooser_dialog_close_cb(self, widget, *args):
115
 
        widget.hide()
116
 
 
117
 
    def _shared_files_file_chooser_dialog_delete_event_cb(self, *args):
118
 
        return True
119
 
 
120
 
    def _add_directory(self, path):
121
 
        if path not in self._shared_directories:
122
 
            self._shared_directories.append(path)
123
 
 
124
 
        self._populate_tree_view()
125
 
 
126
 
    def _populate_tree_view(self):
127
 
        list_store = self._builder.get_object('shared_files_list_store')
128
 
 
129
 
        for path in self._shared_directories:
130
 
            if path not in self._paths_shown:
131
 
                self._paths_shown.add(path)
132
 
                list_store.append([path])
133
 
 
134
 
    def _update_scan_progress(self):
135
 
        if self._scan_task:
136
 
            filename, bytes_read = self._scan_task.progress
137
 
            shared_files_scan_label = self._builder.get_object(
138
 
                'shared_files_scan_label')
139
 
 
140
 
            # FIXME: l10n support
141
 
            shared_files_scan_label.set_text(SCAN_PROGRESS_TEXT.format(
142
 
                filename=filename, bytes_read=bytes_read)
143
 
            )
144
 
 
145
 
            return True
146
 
 
147
 
    def _shared_files_tree_view_cursor_changed_cb(self, tree_view, *args):
148
 
        shared_files_tree_view = self._builder.get_object(
149
 
            'shared_files_tree_view')
150
 
        selection = shared_files_tree_view.get_selection()
151
 
 
152
 
        if selection:
153
 
            sensitive = True if selection.get_selected()[1] else False
154
 
        else:
155
 
            sensitive = False
156
 
 
157
 
        self._builder.get_object('shared_files_remove_button').set_sensitive(
158
 
            sensitive)
159