Skip to content

Commit

Permalink
PuTTy Tray 0.60 v3: File settings backend
Browse files Browse the repository at this point in the history
  • Loading branch information
FauxFaux committed Aug 11, 2013
1 parent 8d91c66 commit bba943f
Show file tree
Hide file tree
Showing 8 changed files with 1,464 additions and 39 deletions.
8 changes: 8 additions & 0 deletions cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ int cmdline_process_param(char *p, char *value, int need_save, Conf *conf)
cmdline_session_name = dupstr(value);
return 2;
}

if (!strcmp(p, "-loadfile") || !strcmp(p, "-file") || !strcmp(p, "-fileload")) {
RETURN(2);
do_defaults_file(value, conf);
loaded_session = TRUE;
return 2;
}

if (!strcmp(p, "-ssh")) {
RETURN(1);
UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
Expand Down
111 changes: 97 additions & 14 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ void conf_fontsel_handler(union control *ctrl, void *dlg,
}
}

struct sessionsaver_data {
union control *editbox, *listbox, *loadbutton, *savebutton, *delbutton;
union control *okbutton, *cancelbutton;
struct sesslist sesslist;
int midsession;
char *savedsession; /* the current contents of ssd->editbox */
};

static void config_host_handler(union control *ctrl, void *dlg,
void *data, int event)
{
Expand Down Expand Up @@ -289,6 +297,52 @@ void config_protocolbuttons_handler(union control *ctrl, void *dlg,
}
}

/*
* HACK: PuttyTray / PuTTY File
* Storagetype radio buttons event handler
*/
void storagetype_handler(union control *ctrl, void *dlg, void *data, int event)
{
int button;
struct sessionsaver_data *ssd =(struct sessionsaver_data *)ctrl->generic.context.p;
Conf *conf = (Conf *)data;

/*
* For a standard radio button set, the context parameter gives
* offsetof(targetfield, Config), and the extra data per button
* gives the value the target field should take if that button
* is the one selected.
*/
if (event == EVENT_REFRESH) {
// Button index = same as storagetype number. Set according to config
button = conf_get_int(conf, CONF_session_storagetype);
dlg_radiobutton_set(ctrl, dlg, button);
} else if (event == EVENT_VALCHANGE) {
button = dlg_radiobutton_get(ctrl, dlg);

// Switch between registry and file
if (ctrl->radio.buttondata[button].i == 0) {
get_sesslist(&ssd->sesslist, FALSE, 0);
get_sesslist(&ssd->sesslist, TRUE, 0);
dlg_refresh(ssd->editbox, dlg);
dlg_refresh(ssd->listbox, dlg);

// Save setting into config (the whole *(int *)ATOFFSET(data, ctrl->radio.context.i) = ctrl->radio.buttondata[button].i; didn't work)
// and I don't see why I shouldn't do it this way (it works?)
conf_set_int(conf, CONF_session_storagetype, 0);
} else {
get_sesslist(&ssd->sesslist, FALSE, 1);
get_sesslist(&ssd->sesslist, TRUE, 1);
dlg_refresh(ssd->editbox, dlg);
dlg_refresh(ssd->listbox, dlg);

// Here as well
conf_set_int(conf, CONF_session_storagetype, 1);
}
}
}
/** HACK: END **/

static void loggingbuttons_handler(union control *ctrl, void *dlg,
void *data, int event)
{
Expand Down Expand Up @@ -557,14 +611,6 @@ static void sshbug_handler(union control *ctrl, void *dlg,
}
}

struct sessionsaver_data {
union control *editbox, *listbox, *loadbutton, *savebutton, *delbutton;
union control *okbutton, *cancelbutton;
struct sesslist sesslist;
int midsession;
char *savedsession; /* the current contents of ssd->editbox */
};

static void sessionsaver_data_free(void *ssdv)
{
struct sessionsaver_data *ssd = (struct sessionsaver_data *)ssdv;
Expand Down Expand Up @@ -674,8 +720,13 @@ static void sessionsaver_handler(union control *ctrl, void *dlg,
sfree(errmsg);
}
}
get_sesslist(&ssd->sesslist, FALSE);
get_sesslist(&ssd->sesslist, TRUE);
/*
* HACK: PuttyTray / PuTTY File
* Added storagetype to get_sesslist
*/
get_sesslist(&ssd->sesslist, FALSE, conf_get_int(conf, CONF_session_storagetype));
get_sesslist(&ssd->sesslist, TRUE, conf_get_int(conf, CONF_session_storagetype));

dlg_refresh(ssd->editbox, dlg);
dlg_refresh(ssd->listbox, dlg);
} else if (!ssd->midsession &&
Expand All @@ -685,8 +736,14 @@ static void sessionsaver_handler(union control *ctrl, void *dlg,
dlg_beep(dlg);
} else {
del_settings(ssd->sesslist.sessions[i]);
get_sesslist(&ssd->sesslist, FALSE);
get_sesslist(&ssd->sesslist, TRUE);

/*
* HACK: PuttyTray / PuTTY File
* Added storagetype to get_sesslist
*/
get_sesslist(&ssd->sesslist, FALSE, conf_get_int(conf, CONF_session_storagetype));
get_sesslist(&ssd->sesslist, TRUE, conf_get_int(conf, CONF_session_storagetype));

dlg_refresh(ssd->listbox, dlg);
}
} else if (ctrl == ssd->okbutton) {
Expand Down Expand Up @@ -1212,7 +1269,7 @@ static void portfwd_handler(union control *ctrl, void *dlg,
}

void setup_config_box(struct controlbox *b, int midsession,
int protocol, int protcfginfo)
int protocol, int protcfginfo, int session_storagetype) // HACK: PuttyTray / PuTTY File - Added 'int session_storagetype'
{
struct controlset *s;
struct sessionsaver_data *ssd;
Expand All @@ -1223,6 +1280,7 @@ void setup_config_box(struct controlbox *b, int midsession,
struct portfwd_data *pfd;
union control *c;
char *str;
int current_storagetype; // HACK: PuttyTray / PuTTY File - stores storagetype after sess_list may or may not have switched between file/registry because registry is empty

ssd = (struct sessionsaver_data *)
ctrl_alloc_with_free(b, sizeof(struct sessionsaver_data),
Expand Down Expand Up @@ -1304,7 +1362,10 @@ void setup_config_box(struct controlbox *b, int midsession,
midsession ? "Save the current session settings" :
"Load, save or delete a stored session");
ctrl_columns(s, 2, 75, 25);
get_sesslist(&ssd->sesslist, TRUE);

// HACK: PuttyTray / PuTTY File - The +2 triggers storagetype autoswitching
current_storagetype = get_sesslist(&ssd->sesslist, TRUE, session_storagetype + (midsession ? 0 : 2));

ssd->editbox = ctrl_editbox(s, "Saved Sessions", 'e', 100,
HELPCTX(session_saved),
sessionsaver_handler, P(ssd), P(NULL));
Expand Down Expand Up @@ -1346,6 +1407,28 @@ void setup_config_box(struct controlbox *b, int midsession,
}
ctrl_columns(s, 1, 100);

/*
* HACK: PuttyTray / PuTTY File
* Add radio buttons
*
* Couldn't get the default selection to switch, so I switched the button position instead.
* Must be the lamest solution I ever came up with.
*
* In midsession, changing causes it to be reversed again (wrong). So don't.
*/
if (midsession || current_storagetype == 0) {
c = ctrl_radiobuttons(s, NULL, 'f', 2,
HELPCTX(no_help),
storagetype_handler,
P(ssd), "Sessions from registry", I(0), "Sessions from file", I(1), NULL);
} else {
c = ctrl_radiobuttons(s, NULL, 'f', 2,
HELPCTX(no_help),
storagetype_handler,
P(ssd), "Sessions from file", I(1), "Sessions from registry", I(0), NULL);
}
/** HACK: END **/

s = ctrl_getset(b, "Session", "otheropts", NULL);
ctrl_radiobuttons(s, "Close window on exit:", 'x', 4,
HELPCTX(session_coe),
Expand Down
12 changes: 10 additions & 2 deletions putty.h
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,7 @@ void cleanup_exit(int);
X(INT, NONE, transparency) \
X(INT, NONE, failure_reconnect) \
X(INT, NONE, wakeup_reconnect) \
X(INT, NONE, session_storagetype) \

/* Now define the actual enum of option keywords using that macro. */
#define CONF_ENUM_DEF(valtype, keytype, keyword) CONF_ ## keyword,
Expand Down Expand Up @@ -935,10 +936,17 @@ char *save_settings(char *section, Conf *conf);
void save_open_settings(void *sesskey, Conf *conf);
void load_settings(char *section, Conf *conf);
void load_open_settings(void *sesskey, Conf *conf);
void get_sesslist(struct sesslist *, int allocate);
int get_sesslist(struct sesslist *, int allocate, int storagetype);
void do_defaults(char *, Conf *);
void registry_cleanup(void);

/*
* HACK: PuttyTray / PuTTY File
* Quick hack to load defaults from file
*/
void do_defaults_file(char *, Conf *);
void load_settings_file(char *section, Conf * cfg);

/*
* Functions used by settings.c to provide platform-specific
* default settings.
Expand Down Expand Up @@ -1256,7 +1264,7 @@ void conf_filesel_handler(union control *ctrl, void *dlg,
void conf_fontsel_handler(union control *ctrl, void *dlg,
void *data, int event);
void setup_config_box(struct controlbox *b, int midsession,
int protocol, int protcfginfo);
int protocol, int protcfginfo, int session_storagetype);

/*
* Exports from minibidi.c.
Expand Down
85 changes: 82 additions & 3 deletions settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ void save_open_settings(void *sesskey, Conf *conf)
write_setting_i(sesskey, "Transparency", conf_get_int(conf, CONF_transparency));
write_setting_i(sesskey, "WakeupReconnect", conf_get_int(conf, CONF_wakeup_reconnect));
write_setting_i(sesskey, "FailureReconnect", conf_get_int(conf, CONF_failure_reconnect));
write_setting_i(sesskey, "StorageType", conf_get_int(conf, CONF_session_storagetype));
write_setting_i(sesskey, "AltF4", conf_get_int(conf, CONF_alt_f4));
write_setting_i(sesskey, "AltSpace", conf_get_int(conf, CONF_alt_space));
write_setting_i(sesskey, "AltOnly", conf_get_int(conf, CONF_alt_only));
Expand Down Expand Up @@ -653,6 +654,19 @@ void load_settings(char *section, Conf *conf)
add_session_to_jumplist(section);
}

/*
* HACK: PuttyTray / PuTTY File
* Quick hack to load defaults from file
*/
void load_settings_file(char *section, Conf * cfg)
{
void *sesskey;
set_storagetype(1);
sesskey = open_settings_r(section);
load_open_settings(sesskey, cfg);
close_settings_r(sesskey);
}

void load_open_settings(void *sesskey, Conf *conf)
{
int i;
Expand Down Expand Up @@ -811,6 +825,7 @@ void load_open_settings(void *sesskey, Conf *conf)
gppi(sesskey, "Transparency", 255, conf, CONF_transparency);
gppi(sesskey, "WakeupReconnect", 0, conf, CONF_wakeup_reconnect);
gppi(sesskey, "FailureReconnect", 0, conf, CONF_failure_reconnect);
gppi(sesskey, "StorageType", 0, conf, CONF_session_storagetype);
gppi(sesskey, "AltF4", 1, conf, CONF_alt_f4);
gppi(sesskey, "AltSpace", 0, conf, CONF_alt_space);
gppi(sesskey, "AltOnly", 0, conf, CONF_alt_only);
Expand Down Expand Up @@ -991,6 +1006,15 @@ void do_defaults(char *session, Conf *conf)
load_settings(session, conf);
}

/*
* HACK: PuttyTray / PuTTY File
* Quick hack to load defaults from file
*/
void do_defaults_file(char *session, Conf * cfg)
{
load_settings_file(session, cfg);
}

static int sessioncmp(const void *av, const void *bv)
{
const char *a = *(const char *const *) av;
Expand All @@ -1011,18 +1035,28 @@ static int sessioncmp(const void *av, const void *bv)
return strcmp(a, b); /* otherwise, compare normally */
}

void get_sesslist(struct sesslist *list, int allocate)
/*
* HACK: PuttyTray / PuTTY File
* Updated get_sesslist with storagetype
*/
int get_sesslist(struct sesslist *list, int allocate, int storagetype) // HACK: PuTTYTray / PuTTY File - changed return type
{
char otherbuf[2048];
int buflen, bufsize, i;
char *p, *ret;
void *handle;

// HACK: PUTTY FILE
int autoswitch = 0;
if (storagetype > 1) {
storagetype = storagetype - 2;
autoswitch = 1;
}

if (allocate) {

buflen = bufsize = 0;
list->buffer = NULL;
if ((handle = enum_settings_start()) != NULL) {
if ((handle = enum_settings_start(storagetype)) != NULL) { // HACK: PuTTYTray / PuTTY File - storagetype
do {
ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
if (ret) {
Expand All @@ -1040,6 +1074,45 @@ void get_sesslist(struct sesslist *list, int allocate)
list->buffer = sresize(list->buffer, buflen + 1, char);
list->buffer[buflen] = '\0';

/*
* HACK: PuttyTray / PuTTY File
* Switch to file mode if registry is empty (and in registry mode)
*/
if (autoswitch == 1 && storagetype != 1 && buflen == 0) {
storagetype = 1;

// Ok, this is a copy of the code above. Crude but working
buflen = bufsize = 0;
list->buffer = NULL;
if ((handle = enum_settings_start(1)) != NULL) { // Force file storage type
do {
ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
if (ret) {
int len = strlen(otherbuf) + 1;
if (bufsize < buflen + len) {
bufsize = buflen + len + 2048;
list->buffer = sresize(list->buffer, bufsize, char);
}
strcpy(list->buffer + buflen, otherbuf);
buflen += strlen(list->buffer + buflen) + 1;
}
} while (ret);
enum_settings_finish(handle);
}
list->buffer = sresize(list->buffer, buflen + 1, char);
list->buffer[buflen] = '\0';
}

/*
* HACK: PuttyTray / PuTTY File
* If registry is empty AND file store is empty, show empty registry
*/
if (autoswitch == 1 && storagetype == 1 && buflen == 0) {
storagetype = 0;
set_storagetype(storagetype);
}


/*
* Now set up the list of sessions. Note that "Default
* Settings" must always be claimed to exist, even if it
Expand Down Expand Up @@ -1075,4 +1148,10 @@ void get_sesslist(struct sesslist *list, int allocate)
list->buffer = NULL;
list->sessions = NULL;
}

/*
* HACK: PuttyTray / PuTTY File
* Return storagetype
*/
return storagetype;
}
8 changes: 7 additions & 1 deletion storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
* elsewhere, since it doesn't (mostly) change between platforms.
*/

/*
* HACK: PuttyTray / PuTTY File
* Function to force the storage loader to a certain type
*/
void set_storagetype(int new_storagetype);

/*
* Write a saved session. The caller is expected to call
* open_setting_w() to get a `void *' handle, then pass that to a
Expand Down Expand Up @@ -66,7 +72,7 @@ void del_settings(const char *sessionname);
/*
* Enumerate all saved sessions.
*/
void *enum_settings_start(void);
void *enum_settings_start(int new_storagetype); // HACK: PuttyTray / PuTTY File - enum_settings_start with storagetype
char *enum_settings_next(void *handle, char *buffer, int buflen);
void enum_settings_finish(void *handle);

Expand Down
Loading

0 comments on commit bba943f

Please sign in to comment.