Skip to content

Commit

Permalink
Startup connection to most recent server
Browse files Browse the repository at this point in the history
  • Loading branch information
tympanix committed Jan 8, 2017
1 parent 5a43c20 commit d68e0f7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
1 change: 0 additions & 1 deletion main.js
Expand Up @@ -28,7 +28,6 @@ torrentApp.constant('$btclients', {
// Configure the client
torrentApp.run(["$rootScope", "$bittorrent", "configService", function($rootScope, $bittorrent, config){
config.initSettings();
$bittorrent.setServer(config.getDefaultServer())
}]);

// Set application menu
Expand Down
23 changes: 15 additions & 8 deletions scripts/controllers/main.js
Expand Up @@ -24,17 +24,24 @@ angular.module("torrentApp").controller("mainController", ["$rootScope", "$scope

if (settings.startup === 'default') {
let server = config.getDefaultServer()
if ($rootScope.$server){
if (server){
connectToServer(server);
} else {
pageServers();
$notify.ok('No default server', 'Please choose a server to connect to')
}
} else if (settings.startup === 'ask') {
pageServers()
} else if (settings.startup === 'latest') {
return
// TODO: Implemented latest server
let server = config.getRecentServer()
console.log("Connecting to last used", server);
if (server){
connectToServer(server)
} else {
pageServers()
$notify.ok('No recent servers', 'Please choose a server to connect to')
}
} else {
/* Ask or unknown*/
pageServers()
}
});

Expand All @@ -51,9 +58,10 @@ angular.module("torrentApp").controller("mainController", ["$rootScope", "$scope
.then(function(){
pageTorrents();
requestMagnetLinks();
})
.catch(function(){
}).catch(function(){
pageSettings('connection');
}).finally(function() {
config.renderServerMenu()
});
}

Expand Down Expand Up @@ -82,7 +90,6 @@ angular.module("torrentApp").controller("mainController", ["$rootScope", "$scope
$scope.showLoading = false;
$scope.$broadcast('start:torrents');
page = null;
console.log("SHOW TORRENTS!", page);
}

function pageLoading() {
Expand Down
1 change: 0 additions & 1 deletion scripts/controllers/settings.js
Expand Up @@ -58,7 +58,6 @@ angular.module("torrentApp").controller("settingsController", ["$rootScope", "$s
config.updateServer($scope.server)
.then(function() {
$bittorrent.setServer($scope.server)
$notify.ok("Server saved", "Saved new server config")
}).catch(function() {
$notify.alert("Settings error", "Could not save new server")
})
Expand Down
2 changes: 2 additions & 0 deletions scripts/services/bittorrent.js
Expand Up @@ -23,6 +23,8 @@ angular.module('torrentApp').service('$bittorrent', ['$rootScope', '$injector',
this.setServer = function(server) {
$rootScope.$btclient = this.getClient(server.client)
$rootScope.$server = server
server.updateLastUsed()
config.saveAllSettings()
console.info("Changed server to:", $rootScope.$server)
console.info("Changed client to:", $rootScope.$btclient)
}
Expand Down
20 changes: 19 additions & 1 deletion scripts/services/config.js
Expand Up @@ -138,6 +138,16 @@ angular.module('torrentApp').service('configService', ['$rootScope', 'notificati
return settings.servers.find(isDefault)
}

this.getRecentServer = function() {
let maxServer = settings.servers[0]
settings.servers.forEach(function(server){
if (server.lastused > maxServer.lastused){
maxServer = server
}
})
return maxServer
}

function getMenu(name) {
let menu = electron.menu.getApplicationMenu()
return menu.items.find((menuItem) => menuItem.label === name)
Expand All @@ -152,13 +162,21 @@ angular.module('torrentApp').service('configService', ['$rootScope', 'notificati
}))
serverMenu.append(new MenuItem({
label: 'Set current as default',
click: () => this.setCurrentServerAsDefault()
click: () => this.setCurrentServerAsDefault(),
enabled: !!$rootScope.$server
}))
serverMenu.append(new MenuItem({type: 'separator'}))
renderServerMenuOptions(serverMenu, this.getServers())
}

function renderServerMenuOptions(menu, servers) {
if (!$rootScope.$server) {
menu.append(new MenuItem({
label: 'Disabled...',
enabled: false
}))
return
}
servers.forEach((server) => {
menu.append(new MenuItem({
label: server.getNameAtAddress(),
Expand Down
9 changes: 8 additions & 1 deletion scripts/services/server.js
Expand Up @@ -15,6 +15,7 @@ angular.module('torrentApp').factory('Server', ['$btclients', function($btclient
this.user = user
this.password = password
this.client = client
this.lastused = -1
}
}

Expand All @@ -26,6 +27,7 @@ angular.module('torrentApp').factory('Server', ['$btclients', function($btclient
this.password = data.password
this.client = data.client
this.default = data.default
this.lastused = data.lastused
};

Server.prototype.json = function () {
Expand All @@ -36,7 +38,8 @@ angular.module('torrentApp').factory('Server', ['$btclients', function($btclient
user: this.user,
password: this.password,
client: this.client,
default: this.default
default: this.default,
lastused: this.lastused || -1
}
};

Expand All @@ -52,6 +55,10 @@ angular.module('torrentApp').factory('Server', ['$btclients', function($btclient
return this.getName() + " @ " + this.ip
};

Server.prototype.updateLastUsed = function () {
this.lastused = new Date().getTime()
};

function generateGUID() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
Expand Down

0 comments on commit d68e0f7

Please sign in to comment.