1
# ecryptfs.nautilus.storage - storage extension for Nautilus
3
# Authors: Tim Cole <tim.cole@canonical.com>
4
# Rodney Dawes <rodney.dawes@canonical.com>
5
# Michael Rooney <mrooney@ubuntu.com>
7
# Copyright 2009 Canonical Ltd.
9
# This program is free software: you can redistribute it and/or modify it
10
# under the terms of the GNU General Public License version 3, as published
11
# by the Free Software Foundation.
13
# This program is distributed in the hope that it will be useful, but
14
# WITHOUT ANY WARRANTY; without even the implied warranties of
15
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
16
# PURPOSE. See the GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License along
19
# with this program. If not, see <http://www.gnu.org/licenses/>.
20
"""Storage extension for Nautilus."""
22
from __future__ import with_statement
27
from urlparse import urlparse
28
from urllib import url2pathname, urlencode
29
from urllib2 import urlopen, Request, HTTPError
30
from twisted.internet import defer
31
from twisted.python import failure
32
from threading import Thread
33
import nautilus_api as nautilus
35
from ecryptfs import ecryptapi
37
class StorageBar(gtk.HBox):
38
"""The storage bar widget."""
40
def __init__(self, path, *args, **kw):
41
"""Initialize the widget."""
42
super(StorageBar, self).__init__(*args, **kw)
43
self.__label = gtk.Label()
44
self.__label.set_markup("These are files in an encrypted directory")
45
self.__label.set_alignment(0.0, 0.5)
47
self.add(self.__label)
48
self.__button = gtk.Button()
49
self.__button.connect("clicked", self.__toggle_state)
51
self.pack_end(self.__button, expand=False, fill=False)
54
self.__update_status()
56
def __toggle_state(self, button):
57
"""Toggle the connectivity state."""
59
ecryptapi.setMounted(False)
61
ecryptapi.setMounted(True)
63
self.__update_status()
65
def __update_status(self):
66
"""Update the label, and button when connection status changes."""
67
self.__mounted = ecryptapi.getMounted()
69
self.__button.set_label("Lock directory")
71
self.__button.set_label("Unlock directory")
74
def is_storagefs(path):
75
"""Returns True if the given path is a directory in a mounted
78
@param path: the path to test
79
@return: True if the path is a directory in storagefs
81
# pylint: disable-msg=W0602
82
if ecryptapi.PRIVATE_LOCATION:
83
return path == ecryptapi.PRIVATE_LOCATION or path.startswith(ecryptapi.PRIVATE_LOCATION + "/")
87
class StorageBarProvider(nautilus.LocationWidgetProvider):
88
"""An extension class providing a location widget for storage
92
# pylint: disable-msg=W0231
93
def __init__(self, widget_class=StorageBar,
94
is_storagefs=is_storagefs):
95
"""Initializes a new instance of the extension class."""
96
self.__widget_class = widget_class
97
self.__storagefs_test = is_storagefs
99
def _get_storage_dir_path(self, url):
100
"""Gets the local filesystem path corresponding to the given URL,
101
or otherwise None if it does not refer to a storage directory.
103
@param url: the directory URL
104
@return: the local filesystem path, or else None
107
parsed_url = urlparse(url)
108
if parsed_url.scheme == "file" and parsed_url.path:
109
path = url2pathname(parsed_url.path)
110
if self.__storagefs_test(path):
117
def get_widget(self, url, window):
118
"""Returns either None or a Gtk widget to decorate the Nautilus
119
window with, based on whether the current directory is a storage
122
@param url: the URL of the currently viewed directory
123
@param window: the Nautilus window
124
@return: a Gtk widget or None
127
path = self._get_storage_dir_path(url)
129
widget = self.__widget_class(path=path)