22
22
from __future__ import division
24
from PyQt4 import QtGui, QtCore
24
from PyQt4 import QtCore
25
25
from twisted.internet import defer
27
from ubuntuone.controlpanel import backend
27
from ubuntuone.controlpanel.backend import AUTOCONNECT_KEY
28
28
from ubuntuone.controlpanel.logger import setup_logging, log_call
29
29
from ubuntuone.controlpanel.gui import (
39
from ubuntuone.controlpanel.gui.qt import uri_hook
40
from ubuntuone.controlpanel.gui import qt
41
from ubuntuone.controlpanel.gui.qt.ubuntuonebin import UbuntuOneBin
40
42
from ubuntuone.controlpanel.gui.qt.ui import controlpanel_ui
43
45
logger = setup_logging('qt.controlpanel')
45
NAME_STYLE = '<br><span style=" font-size:24pt;">%s!</span>'
47
NAME_STYLE = '<br><span style=" font-size:16pt;">%s!</span>'
46
48
PERCENTAGE_STYLE = '<span style=" font-size:16pt;">%.0f%%</span>'
49
class ControlPanel(QtGui.QWidget):
51
class ControlPanel(UbuntuOneBin):
50
52
"""The Control Panel widget"""
52
def __init__(self, parent=None):
53
"""Initialize the UI of the widget."""
54
QtGui.QWidget.__init__(self, parent)
55
self.ui = controlpanel_ui.Ui_Form()
58
self.backend = backend.ControlBackend()
60
logger.debug('%s: started.', self.__class__.__name__)
54
ui_class = controlpanel_ui
63
58
"""Do some extra setupping for the UI."""
64
self.ui.get_more_space_button.uri = EDIT_ACCOUNT_LINK
59
self.ui.get_more_space_button.uri = EDIT_SERVICES_LINK
65
60
self.ui.help_button.uri = GET_SUPPORT_LINK
67
# Invalid name "showEvent"
68
# pylint: disable=C0103
70
def showEvent(self, event):
61
self.ui.devices_tab.localDeviceRemoved.connect(
62
self.on_credentials_not_found)
63
self.ui.signin.credentialsFound.connect(lambda creds: self.load())
65
@defer.inlineCallbacks
66
def connect_file_sync(self):
67
"""Connect file sync service if the setting autoconnect is enabled."""
68
settings = yield self.backend.file_sync_settings_info()
69
if AUTOCONNECT_KEY in settings and settings[AUTOCONNECT_KEY]:
70
yield self.backend.connect_files()
72
@log_call(logger.debug)
73
def on_credentials_not_found(self):
74
"""Credentials are not found or were removed."""
75
self.ui.switcher.setCurrentWidget(self.ui.signin)
76
self.is_processing = False
78
@log_call(logger.debug)
79
def on_credentials_found(self):
80
"""Credentials are not found or were removed."""
81
self.ui.switcher.setCurrentWidget(self.ui.management)
82
self.connect_file_sync()
83
self.is_processing = False
85
# pylint: disable=E0202
75
86
@defer.inlineCallbacks
78
info = yield self.backend.account_info()
79
self.process_info(info)
89
self.is_processing = True
90
credentials = yield self.backend.get_credentials()
92
self.on_credentials_not_found()
94
self.on_credentials_found()
95
info = yield self.backend.account_info()
96
self.process_info(info)
81
98
@log_call(logger.debug)
82
99
def process_info(self, info):
87
104
used = int(info['quota_used'])
88
105
total = int(info['quota_total'])
89
percentage = {'percentage': PERCENTAGE_STYLE % ((used / total) * 100)}
106
percentage_value = ((used / total) * 100)
107
percentage = {'percentage': PERCENTAGE_STYLE % percentage_value}
90
108
data = {'used': humanize(used), 'total': humanize(total)}
91
109
self.ui.percentage_usage_label.setText(PERCENTAGE_LABEL % percentage)
92
110
self.ui.quota_usage_label.setText(USAGE_LABEL % data)
111
self._update_quota({'percentage': percentage_value})
113
@log_call(logger.debug)
114
def _update_quota(self, data=None):
115
"""Update the quota info."""
118
fraction = data.get('percentage', 0.0) / 100
119
if fraction > 0 and fraction < 0.05:
122
fraction = round(fraction, 2)
124
logger.debug('ManagementPanel: updating quota to %r.', fraction)
125
self.ui.percentage_usage_label.setProperty("OverQuota",
126
fraction >= QUOTA_THRESHOLD)
127
self.ui.quota_usage_label.setProperty("OverQuota",
128
fraction >= QUOTA_THRESHOLD)
129
self.ui.percentage_usage_label.style().unpolish(
130
self.ui.percentage_usage_label)
131
self.ui.percentage_usage_label.style().polish(
132
self.ui.percentage_usage_label)
133
self.ui.quota_usage_label.style().unpolish(
134
self.ui.quota_usage_label)
135
self.ui.quota_usage_label.style().polish(self.ui.quota_usage_label)
94
137
@QtCore.pyqtSlot()
95
138
def on_twitter_button_clicked(self):
96
139
"""The twitter button was clicked."""
97
uri_hook(TWITTER_LINK)
140
qt.uri_hook(TWITTER_LINK)
99
142
@QtCore.pyqtSlot()
100
143
def on_facebook_button_clicked(self):
101
144
"""The facebook button was clicked."""
102
uri_hook(FACEBOOK_LINK)
145
qt.uri_hook(FACEBOOK_LINK)