Mercurial > hg > ltpda-connection-manager
comparison LTPDAConnectionManager.m @ 5:35f1cfcaa5a9
Add crude Java GUI.
author | Daniele Nicolodi <daniele@science.unitn.it> |
---|---|
date | Wed, 02 Jun 2010 17:29:18 +0200 |
parents | c706c10a76bd |
children |
comparison
equal
deleted
inserted
replaced
4:c706c10a76bd | 5:35f1cfcaa5a9 |
---|---|
397 | 397 |
398 | 398 |
399 function [username, password, cache] = inputCredentials(cm, cred) | 399 function [username, password, cache] = inputCredentials(cm, cred) |
400 % INPUTCREDENTIALS Queries the user for database username and password. | 400 % INPUTCREDENTIALS Queries the user for database username and password. |
401 | 401 |
402 % this is a stubb that must be replaced by a graphical interface | |
403 | |
404 % build a cell array of usernames and passwords | 402 % build a cell array of usernames and passwords |
405 users = { cred(:).username }; | 403 users = { cred(:).username }; |
406 passw = { cred(:).password }; | 404 passw = { cred(:).password }; |
407 | 405 |
408 % default to the latest used username | 406 % sort on the expiry time |
409 [e, ids] = sort([ cred(:).expiry ]); | 407 [e, ids] = sort([ cred(:).expiry ]); |
410 default = users{ids(1)}; | 408 users = users{ids} |
411 | 409 passw = passw{ids} |
412 username = choose('Username', users, default); | 410 |
413 | 411 dialog = connectionmanager.CredentialsDialog(users, passw); |
414 % pick the corresponding password | 412 dialog.show(); |
415 ids = find(strcmp(users, username)); | 413 if dialog.cancelled |
416 if ~isempty(ids) | 414 throw(MException('utils:jmysql:connect:UserCancelled', '### user cancelled'); |
417 default = passw{ids(1)}; | 415 end |
418 else | 416 username = char(dialog.username); |
419 default = []; | 417 password = char(dialog.password); |
420 end | 418 cache = logical(dialog.cache); |
421 | |
422 password = ask('Password', ''); | |
423 | |
424 if cm.cachePassword == 2 | |
425 cache = ask('Store credentials', 'n'); | |
426 if ~isempty(cache) && cache(1) == 'y' | |
427 cache = true; | |
428 else | |
429 cache = false; | |
430 end | |
431 else | |
432 cache = logical(cm.cachePassword); | |
433 end | |
434 end | 419 end |
435 | 420 |
436 | 421 |
437 function [hostname, database, username] = selectDatabase(cm) | 422 function [hostname, database, username] = selectDatabase(cm) |
438 % SELECTDATABASE Makes the user choose to which database connect to. | 423 % SELECTDATABASE Makes the user choose to which database connect to. |
439 | 424 |
440 % this is a stubb that must be replaced by a graphical interface | 425 dialog = connectionmanager.DatabaseSelectorDialog(); |
441 | 426 for c = cm.credentials |
442 for kk = 1:numel(cm.credentials) | 427 dialog.add(c{1}.hostname, c{1}.database, c{1}.username); |
443 fprintf('% 2d. %s\n', char(cm.credentials{kk})); | 428 end |
444 end | 429 dialog.show(); |
445 fprintf('%d. NEW (default)\n', numel(cm.credentials)+1); | 430 if dialog.cancelled |
446 str = input('Select connection: ', 's'); | 431 throw(MException('utils:jmysql:connect:UserCancelled', '### user cancelled'); |
447 if isempty(str) | 432 end |
448 id = numel(cm.credentials)+1; | 433 hostname = char(dialog.hostname); |
449 else | 434 database = char(dialog.database); |
450 id = eval(str); | 435 username = char(dialog.username); |
451 end | |
452 if id > numel(cm.credentials) | |
453 hostname = input('Hostname: ', 's'); | |
454 database = input('Database: ', 's'); | |
455 username = []; | |
456 else | |
457 hostname = cm.credentials{kk}.hostname; | |
458 database = cm.credentials{kk}.database; | |
459 username = cm.credentials{kk}.username; | |
460 end | |
461 end | 436 end |
462 | 437 |
463 end % private methods | 438 end % private methods |
464 | 439 |
465 end % classdef | 440 end % classdef |
498 end | 473 end |
499 end | 474 end |
500 rethrow(ex); | 475 rethrow(ex); |
501 end | 476 end |
502 end | 477 end |
503 | |
504 | |
505 function str = ask(msg, default) | |
506 str = input(sprintf('%s (default: %s): ', msg, default), 's'); | |
507 if isempty(str) | |
508 str = default; | |
509 end | |
510 if ~ischar(str) | |
511 str = char(str); | |
512 end | |
513 end | |
514 | |
515 function str = choose(msg, choices, default) | |
516 options = sprintf('%s, ', choices{:}); | |
517 options = options(1:end-2); | |
518 str = input(sprintf('%s (options: %s, default: %s): ', msg, options, default), 's'); | |
519 if isempty(str) | |
520 str = default; | |
521 end | |
522 if ~ischar(str) | |
523 str = char(str); | |
524 end | |
525 end |