Carbonise. Until we support Navigation Services, loading and saving sessions
[u/mdw/putty] / mac / macdlg.c
index a7c3c3f..56f2d9a 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: macdlg.c,v 1.1 2002/12/31 01:40:14 ben Exp $ */
+/* $Id: macdlg.c,v 1.9 2003/02/01 23:55:00 ben Exp $ */
 /*
  * Copyright (c) 2002 Ben Harris
  * All rights reserved.
  */
 
 #include <MacTypes.h>
+#include <AEDataModel.h>
+#include <AppleEvents.h>
 #include <Dialogs.h>
+#include <Resources.h>
+#include <StandardFile.h>
 #include <Windows.h>
 
+#include <assert.h>
 #include <string.h>
 
 #include "putty.h"
 #include "mac.h"
 #include "macresid.h"
+#include "storage.h"
 
 void mac_newsession(void)
 {
@@ -47,14 +53,179 @@ void mac_newsession(void)
     s = smalloc(sizeof(*s));
     memset(s, 0, sizeof(*s));
     do_defaults(NULL, &s->cfg);
-    s->back = &loop_backend;
+    s->hasfile = FALSE;
 
-    s->settings_window = GetNewDialog(wSettings, NULL, (WindowPtr)-1);
+    s->settings_window =
+       GetDialogWindow(GetNewDialog(wSettings, NULL, (WindowPtr)-1));
 
     SetWRefCon(s->settings_window, (long)s);
     ShowWindow(s->settings_window);
 }
 
+void mac_dupsession(void)
+{
+    Session *s1 = (Session *)GetWRefCon(FrontWindow());
+    Session *s2;
+
+    s2 = smalloc(sizeof(*s2));
+    memset(s2, 0, sizeof(*s2));
+    s2->cfg = s1->cfg;
+    s2->hasfile = s1->hasfile;
+    s2->savefile = s1->savefile;
+
+    mac_startsession(s2);
+}
+
+static OSErr mac_opensessionfrom(FSSpec *fss)
+{
+    FInfo fi;
+    Session *s;
+    void *sesshandle;
+    OSErr err;
+
+    s = smalloc(sizeof(*s));
+    memset(s, 0, sizeof(*s));
+
+    err = FSpGetFInfo(fss, &fi);
+    if (err != noErr) return err;
+    if (fi.fdFlags & kIsStationery)
+       s->hasfile = FALSE;
+    else {
+       s->hasfile = TRUE;
+       s->savefile = *fss;
+    }
+
+    sesshandle = open_settings_r_fsp(fss);
+    if (sesshandle == NULL) {
+       /* XXX need a way to pass up an error number */
+       err = -9999;
+       goto fail;
+    }
+    load_open_settings(sesshandle, TRUE, &s->cfg);
+    close_settings_r(sesshandle);
+
+    mac_startsession(s);
+    return noErr;
+
+  fail:
+    sfree(s);
+    return err;
+}
+
+void mac_opensession(void)
+{
+#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
+    StandardFileReply sfr;
+    static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
+
+    StandardGetFile(NULL, 1, sftypes, &sfr);
+    if (!sfr.sfGood) return;
+
+    mac_opensessionfrom(&sfr.sfFile);
+    /* XXX handle error */
+#endif
+}
+
+void mac_savesession(void)
+{
+    Session *s = (Session *)GetWRefCon(FrontWindow());
+    void *sesshandle;
+
+    assert(s->hasfile);
+    sesshandle = open_settings_w_fsp(&s->savefile);
+    if (sesshandle == NULL) return; /* XXX report error */
+    save_open_settings(sesshandle, TRUE, &s->cfg);
+    close_settings_w(sesshandle);
+}
+
+void mac_savesessionas(void)
+{
+#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
+    Session *s = (Session *)GetWRefCon(FrontWindow());
+    StandardFileReply sfr;
+    void *sesshandle;
+
+    StandardPutFile("\pSave session as:",
+                   s->hasfile ? s->savefile.name : "\puntitled", &sfr);
+    if (!sfr.sfGood) return;
+
+    if (!sfr.sfReplacing) {
+       FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
+       if (ResError() != noErr) return; /* XXX report error */
+    }
+    sesshandle = open_settings_w_fsp(&sfr.sfFile);
+    if (sesshandle == NULL) return; /* XXX report error */
+    save_open_settings(sesshandle, TRUE, &s->cfg);
+    close_settings_w(sesshandle);
+    s->hasfile = TRUE;
+    s->savefile = sfr.sfFile;
+#endif
+}
+
+pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
+                          long refcon)
+{
+    DescType type;
+    Size size;
+
+    if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
+                         &type, NULL, 0, &size) == noErr)
+       return errAEParamMissed;
+
+    /* XXX we should do something here. */
+    return noErr;
+}
+
+pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
+                          long refcon)
+{
+    DescType type;
+    AEKeyword keywd;
+    Size size;
+    AEDescList docs = { typeNull, NULL };
+    OSErr err;
+    long ndocs, i;
+    FSSpec fss;
+
+    err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
+    if (err != noErr) goto out;
+
+    if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
+                         &type, NULL, 0, &size) == noErr) {
+       err = errAEParamMissed;
+       goto out;
+    }
+
+    err = AECountItems(&docs, &ndocs);
+    if (err != noErr) goto out;
+
+    for (i = 0; i < ndocs; i++) {
+       err = AEGetNthPtr(&docs, i + 1, typeFSS,
+                         &keywd, &type, &fss, sizeof(fss), &size);
+       if (err != noErr) goto out;
+       err = mac_opensessionfrom(&fss);
+       if (err != noErr) goto out;
+    }
+
+  out:
+    AEDisposeDesc(&docs);
+    return err;
+}
+
+pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply,
+                          long refcon)
+{
+    DescType type;
+    Size size;
+
+    if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
+                         &type, NULL, 0, &size) == noErr)
+       return errAEParamMissed;
+
+    /* We can't meaningfully do anything here. */
+    return errAEEventNotHandled;
+}
+
 void mac_activatedlg(WindowPtr window, EventRecord *event)
 {
     DialogItemType itemtype;
@@ -62,22 +233,24 @@ void mac_activatedlg(WindowPtr window, EventRecord *event)
     short item;
     Rect itemrect;
     int active;
+    DialogRef dialog = GetDialogFromWindow(window);
 
     active = (event->modifiers & activeFlag) != 0;
-    GetDialogItem(window, wiSettingsOpen, &itemtype, &itemhandle, &itemrect);
+    GetDialogItem(dialog, wiSettingsOpen, &itemtype, &itemhandle, &itemrect);
     HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
-    DialogSelect(event, &window, &item);
+    DialogSelect(event, &dialog, &item);
 }
 
 void mac_clickdlg(WindowPtr window, EventRecord *event)
 {
     short item;
     Session *s = (Session *)GetWRefCon(window);
+    DialogRef dialog = GetDialogFromWindow(window);
 
-    if (DialogSelect(event, &(DialogPtr)window, &item))
+    if (DialogSelect(event, &dialog, &item))
        switch (item) {
          case wiSettingsOpen:
-           CloseWindow(window);
+           HideWindow(window);
            mac_startsession(s);
            break;
        }