1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# ecryptfs.nautilus.storage - storage extension for Nautilus
#
# Authors: Tim Cole <tim.cole@canonical.com>
# Rodney Dawes <rodney.dawes@canonical.com>
# Michael Rooney <mrooney@ubuntu.com>
#
# Copyright 2009 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Storage extension for Nautilus."""
from __future__ import with_statement
import os
import gtk
import gnomekeyring
from urlparse import urlparse
from urllib import url2pathname, urlencode
from urllib2 import urlopen, Request, HTTPError
from twisted.internet import defer
from twisted.python import failure
from threading import Thread
import nautilus_api as nautilus
from ecryptfs import ecryptapi, ecryptui
LOCK_SECURE_SMALL = os.path.join(os.getcwd(), "ecryptfs/nautilus/lock-secure-small.png")
LOCK_INSECURE_SMALL = os.path.join(os.getcwd(), "ecryptfs/nautilus/lock-insecure-small.png")
class StorageBar(gtk.HBox):
"""The storage bar widget."""
def __init__(self, path, *args, **kw):
"""Initialize the widget."""
super(StorageBar, self).__init__(*args, **kw)
self.__path = path
self.__mounted = None
# Create the left-side label text.
self.__label = gtk.Label()
self.__label.set_markup("These are files in an encrypted directory")
self.__label.set_alignment(0.0, 0.5)
self.__label.show()
# Initialize the lock/unlock button and image.
self.__button = gtk.Button()
self.__button.set_property("image-position", gtk.POS_LEFT)
self.__button.connect("clicked", self.__toggle_state)
self.__button.show()
self.__button_image = gtk.Image()
# Initialize the configure button.
self.__button_settings = gtk.Button("Configure")
self.__button_settings.connect("clicked", self.__onclick_configure)
self.__button_settings.show()
# Layout everything
self.add(self.__label)
self.pack_start(self.__button, expand=False, fill=False)
self.pack_end(self.__button_settings, expand=False, fill=False)
# Update the status appropriately.
self.__update_status()
def __toggle_state(self, button):
"""Toggle the connectivity state."""
if self.__mounted:
ecryptapi.set_mounted(False)
else:
ecryptapi.set_mounted(True)
self.__update_status()
def __onclick_configure(self, button):
ecryptui.main()
def __update_status(self):
"""Update the label, and button when connection status changes."""
self.__mounted = ecryptapi.get_mounted()
if self.__mounted:
label = "Lock directory"
img = LOCK_SECURE_SMALL
else:
label = "Unlock directory"
img = LOCK_INSECURE_SMALL
self.__button_image.set_from_file(img)
self.__button.set_image(self.__button_image)
self.__button.set_label(label)
def is_storagefs(path):
"""Returns True if the given path is a directory in a mounted
storagefs filesystem.
@param path: the path to test
@return: True if the path is a directory in storagefs
"""
# pylint: disable-msg=W0602
if ecryptapi.PRIVATE_LOCATION:
return path == ecryptapi.PRIVATE_LOCATION or path.startswith(ecryptapi.PRIVATE_LOCATION + "/")
else:
return False
class EcryptfsBarProvider(nautilus.LocationWidgetProvider):
"""An extension class providing a location widget for storage
directories.
"""
# pylint: disable-msg=W0231
def __init__(self, widget_class=StorageBar,
is_storagefs=is_storagefs):
"""Initializes a new instance of the extension class."""
self.__widget_class = widget_class
self.__storagefs_test = is_storagefs
def _get_storage_dir_path(self, url):
"""Gets the local filesystem path corresponding to the given URL,
or otherwise None if it does not refer to a storage directory.
@param url: the directory URL
@return: the local filesystem path, or else None
"""
parsed_url = urlparse(url)
if parsed_url.scheme == "file" and parsed_url.path:
path = url2pathname(parsed_url.path)
if self.__storagefs_test(path):
return path
else:
return None
else:
return None
def get_widget(self, url, window):
"""Returns either None or a Gtk widget to decorate the Nautilus
window with, based on whether the current directory is a storage
directory.
@param url: the URL of the currently viewed directory
@param window: the Nautilus window
@return: a Gtk widget or None
"""
path = self._get_storage_dir_path(url)
if path is not None:
widget = self.__widget_class(path=path)
widget.show()
return widget
else:
return None
|