Commit 8ed2120a authored by Stavros Sachtouris's avatar Stavros Sachtouris Committed by Giorgos Korfiatis
Browse files

Create and save settings is they are missing

If some settings are missing (e.g., not found in the configuration
file), agkyra GUI will run and let the user complete them through
the "setting" menu option. If the default_sync, sync or cloud
options are missing, they are created automatically.

Settings are saved only if the user opens the "settings" window.
parent d25b5b4b
...@@ -45,7 +45,6 @@ ...@@ -45,7 +45,6 @@
var removed_at_least_one = false var removed_at_least_one = false
win.cookies.getAll({url: url}, function(cookies) { win.cookies.getAll({url: url}, function(cookies) {
$.each(cookies, function(i, cookie) { $.each(cookies, function(i, cookie) {
console.log('I have a cookie to remove ' + cookie.name + ', ' + cookie.domain);
win.cookies.remove({url: url, name: cookie.name} ); win.cookies.remove({url: url, name: cookie.name} );
removed_at_least_one = true; removed_at_least_one = true;
}); });
...@@ -56,9 +55,7 @@ ...@@ -56,9 +55,7 @@
var gui = require('nw.gui'); var gui = require('nw.gui');
var cred_win = null; var cred_win = null;
var logout_win = null; var logout_win = null;
var can_close_cred = false;
var got_cookie = false; var got_cookie = false;
var can_close_logout = false;
var show_creds = true; var show_creds = true;
function get_credentials() { function get_credentials() {
var cookie_name = '_pithos2_a'; var cookie_name = '_pithos2_a';
...@@ -80,7 +77,7 @@ ...@@ -80,7 +77,7 @@
} }
}); });
cred_win.on('loaded', function() { cred_win.on('loaded', function() {
if (got_cookie) can_close_cred = true; if (got_cookie) cred_win.close();
}); });
cred_win.on('closed', function() { cred_win.on('closed', function() {
...@@ -89,10 +86,9 @@ ...@@ -89,10 +86,9 @@
{focus: true, width:20, height: 20 }); {focus: true, width:20, height: 20 });
logout_win.hide(); logout_win.hide();
logout_win.on('loaded', function() { logout_win.on('loaded', function() {
while( while(remove_cookies(logout_win, get_pithos_ui())) {}
remove_cookies(logout_win, get_account_ui()) || logout_win.close();
remove_cookies(logout_win, get_pithos_ui())) {} show_creds = true;
can_close_logout = true;
}); });
}); });
} }
...@@ -103,16 +99,6 @@ ...@@ -103,16 +99,6 @@
if (get_pithos_ui() && show_creds) { if (get_pithos_ui() && show_creds) {
$('#get_creds').show(); $('#get_creds').show();
} else {$('#get_creds').hide(); } } else {$('#get_creds').hide(); }
// Garbage collector
if (can_close_logout) {
can_close_logout = false;
logout_win.close();
show_creds = true;
}
else if (can_close_cred) {
can_close_cred = false;
cred_win.close();
}
}, 500); }, 500);
</script> </script>
</head> </head>
......
...@@ -64,15 +64,42 @@ class WebSocketProtocol(WebSocket): ...@@ -64,15 +64,42 @@ class WebSocketProtocol(WebSocket):
settings = dict( settings = dict(
token=None, url=None, token=None, url=None,
container=None, directory=None, container=None, directory=None,
exclude=None, pithos_ui=None) exclude=None)
status = dict(progress=0, paused=True) status = dict(progress=0, paused=True)
file_syncer = None file_syncer = None
cnf = AgkyraConfig() cnf = AgkyraConfig()
def _get_default_sync(self):
"""Get global.default_sync or pick the first sync as default
If there are no syncs, create a 'default' sync.
"""
sync = self.cnf.get('global', 'default_sync')
if not sync:
for sync in self.cnf.keys('sync'):
break
self.cnf.set('global', 'default_sync', sync or 'default')
return sync or 'default'
def _get_sync_cloud(self, sync):
"""Get the <sync>.cloud or pick the first cloud and use it
In case of cloud picking, set the cloud as the <sync>.cloud for future
sessions.
If no clouds are found, create a 'default' cloud, with an empty url.
"""
try:
cloud = self.cnf.get_sync(sync, 'cloud')
except KeyError:
cloud = None
if not cloud:
for cloud in self.cnf.keys('cloud'):
break
self.cnf.set_sync(sync, 'cloud', cloud or 'default')
return cloud or 'default'
def _load_settings(self): def _load_settings(self):
LOG.debug('Start loading settings') LOG.debug('Start loading settings')
sync = self.cnf.get('global', 'default_sync') sync = self._get_default_sync()
cloud = self.cnf.get_sync(sync, 'cloud') cloud = self._get_sync_cloud(sync)
try: try:
self.settings['url'] = self.cnf.get_cloud(cloud, 'url') self.settings['url'] = self.cnf.get_cloud(cloud, 'url')
...@@ -84,16 +111,27 @@ class WebSocketProtocol(WebSocket): ...@@ -84,16 +111,27 @@ class WebSocketProtocol(WebSocket):
self.settings['url'] = None self.settings['url'] = None
for option in ('container', 'directory', 'exclude'): for option in ('container', 'directory', 'exclude'):
self.settings[option] = self.cnf.get_sync(sync, option) try:
self.settings[option] = self.cnf.get_sync(sync, option)
except KeyError:
LOG.debug('No %s is set' % option)
LOG.debug('Finished loading settings') LOG.debug('Finished loading settings')
def _dump_settings(self): def _dump_settings(self):
LOG.debug('Saving settings') LOG.debug('Saving settings')
sync = self.cnf.get('global', 'default_sync') if not self.settings.get('url', None):
cloud = self.cnf.get_sync(sync, 'cloud') LOG.debug('No settings to save')
return
sync = self._get_default_sync()
cloud = self._get_sync_cloud(sync)
try:
old_url = self.cnf.get_cloud(cloud, 'url') or ''
except KeyError:
old_url = self.settings['url']
old_url = self.cnf.get_cloud(cloud, 'url') or ''
while old_url != self.settings['url']: while old_url != self.settings['url']:
cloud = '%s_%s' % (cloud, sync) cloud = '%s_%s' % (cloud, sync)
try: try:
...@@ -102,11 +140,11 @@ class WebSocketProtocol(WebSocket): ...@@ -102,11 +140,11 @@ class WebSocketProtocol(WebSocket):
break break
self.cnf.set_cloud(cloud, 'url', self.settings['url']) self.cnf.set_cloud(cloud, 'url', self.settings['url'])
self.cnf.set_cloud(cloud, 'token', self.settings['token']) self.cnf.set_cloud(cloud, 'token', self.settings['token'] or '')
self.cnf.set_sync(sync, 'cloud', cloud) self.cnf.set_sync(sync, 'cloud', cloud)
for option in ('directory', 'container', 'exclude'): for option in ('directory', 'container', 'exclude'):
self.cnf.set_sync(sync, option, self.settings[option]) self.cnf.set_sync(sync, option, self.settings[option] or '')
self.cnf.write() self.cnf.write()
LOG.debug('Settings saved') LOG.debug('Settings saved')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment