SaveObject 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : SaveObject.c                           */
/*                                                 */
/* Purpose: Handle the Save Object dialogue        */
/*          (actual object saving is done in       */
/*          Save.c).                               */
/*                                                 */
/* Author : A.D.Hodgkinson                         */
/*                                                 */
/* History: 03-Sep-97: Created.                    */
25 26 27 28 29
/*                                                 */
/*          05-Sep-97: Split so bulk became the    */
/*                     SaveFile source, and the    */
/*                     small fetch saving stayed   */
/*                     in here.                    */
30 31 32 33 34 35 36
/***************************************************/

#include <stdlib.h>
#include <string.h>

#include "swis.h"

37 38
#include "HTMLLib.h" /* HTML library API, Which will include html2_ext.h, tags.h and struct.h */

39 40 41 42 43 44
#include "wimp.h"
#include "wimplib.h"
#include "event.h"

#include "toolbox.h"
#include "window.h"
45
#include "gadgets.h"
46 47 48 49 50

#include "svcprint.h"
#include "Global.h"
#include "Utils.h"

51 52 53
#include "Browser.h"
#include "Fetch.h"  /* (For ISLINK macro) */
#include "Protocols.h"
54
#include "Save.h"
55 56
#include "SaveFile.h"
#include "Windows.h"
57

58
#include "SaveObject.h"
59

60
/* Static function prototypes */
61

62 63 64
static int saveobject_drag_ended (int eventcode, ToolboxEvent * event, IdBlock * idb, browser_data * handle);
static int saveobject_ok         (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int saveobject_cancel     (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
65

66 67 68 69 70 71 72 73 74 75
/*************************************************/
/* saveobject_open_for()                         */
/*                                               */
/* Creates and opens a Save Object dialogue for  */
/* a given browser, opening near the pointer.    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which is the ancestor of the      */
/*             dialogue.                         */
/*************************************************/
76

77 78
_kernel_oserror * saveobject_open_for(browser_data * b)
{
79
  ObjectId id;
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  /* Create and show the object */

  RetError(toolbox_create_object(0,
                                 "SaveObject",
                                 &id));

  /* Remember the ID */

  b->save_dbox = id;

  /* Show it - everything else is done when the ToBeShown */
  /* event comes in.                                      */

  RetError(toolbox_show_object(0,
                               id,
                               Toolbox_ShowObject_Centre,
                               NULL,
                               b->self_id,
                               -1));

  return NULL;
}
103 104 105 106 107

/*************************************************/
/* saveobject_to_be_shown()                      */
/*                                               */
/* Fills in the Save Object dialogue prior to    */
108
/* being shown.                                  */
109 110 111 112 113 114 115
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int saveobject_to_be_shown(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
116 117 118 119 120 121 122 123 124 125 126 127 128
  char           text[Limits_OS_Pathname];
  browser_data * b;

  /* Process the writable icon text */

  ChkError(windows_process_component_text(idb->self_id, SaveObjectWrit, text, sizeof(text), 0, 1));

  /* Where did we come from? */

  ChkError(toolbox_get_client_handle(0, idb->ancestor_id, (void *) &b));

  #ifndef TRACE

129
    if (!is_known_browser(b)) return 0;
130 131 132

  #else

133
    if (!is_known_browser(b))
134 135 136
    {
      erb.errnum = 0;
      sprintf(erb.errmess,
137
              "Save dialogue origin of object is suspect; given browser %p is unknown, in saveobject_to_be_shown",
138 139 140 141 142 143 144 145 146 147 148 149 150
              b);

      show_error_ret(&erb);

      return 0;
    }

  #endif

  ChkError(savefile_set_leafname_from_url(idb->self_id, SaveObjectWrit, browser_fetch_url(b)));

  /* Set the draggable sprite */

151
  ChkError(savefile_set_filetype(idb->self_id, SaveObjectDrag, b->save_type, 1));
152 153 154 155 156 157 158 159

  /* Install required event handlers */

  ChkError(event_register_toolbox_handler(idb->self_id, Draggable_DragEnded, (ToolboxEventHandler *) saveobject_drag_ended, b));

  ChkError(event_register_toolbox_handler(idb->self_id, ESaveObjectOK,     saveobject_ok,     b));
  ChkError(event_register_toolbox_handler(idb->self_id, ESaveObjectCancel, saveobject_cancel, b));

160 161 162 163
  return 1;
}

/*************************************************/
164
/* saveobject_drag_ended()                       */
165
/*                                               */
166 167
/* Handle Draggable_DragEnded events from the    */
/* Save Object dialogue draggable sprite gadget. */
168 169 170 171 172
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

173
static int saveobject_drag_ended(int eventcode, ToolboxEvent * event, IdBlock * idb, browser_data * handle)
174
{
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  DraggableDragEndedEvent * drag = (DraggableDragEndedEvent *) event;
  WimpGetPointerInfoBlock   info;
  int                       window_handle;
  char                      path[Limits_OS_Pathname];
  char                    * leaf;

  /* If the user dragged back to the save dialogue, do nothing */

  ChkError(window_get_wimp_handle(0,
                                  idb->self_id,
                                  &window_handle));

  if (window_handle == drag->window_handle) return 1;

  /* Get the pathname from the Save Object dialogue. */

  ChkError(writablefield_get_value(0,
                                   idb->self_id,
                                   SaveObjectWrit,
                                   path,
                                   sizeof(path),
                                   NULL));

  path[sizeof(path) - 1] = 0;

  /* Point to the leafname component */

  leaf = strrchr(path, '.');
  if (!leaf) leaf = path;
  else       leaf ++;

  /* Send out the DataSave message */

  info.x             = drag->x;
  info.y             = drag->y;
  info.window_handle = drag->window_handle;
  info.icon_handle   = drag->icon_handle;

  ChkError(protocols_atats_send_data_save(handle,
                                          NULL,
                                          leaf,
                                          save_object_size(handle),
                                          handle->save_type,
                                          protocols_saving_object,
                                          &info));
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  return 1;
}

/*************************************************/
/* saveobject_ok()                               */
/*                                               */
/* Handles clicks on the 'OK' button in the      */
/* Save Object dialogue.                         */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

static int saveobject_ok(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  char           path[Limits_OS_Pathname];
  char         * leaf;
  browser_data * b;

  /* Get the pathname from the Save Object dialogue. */

  ChkError(writablefield_get_value(0,
                                   idb->self_id,
                                   SaveObjectWrit,
                                   path,
                                   sizeof(path),
                                   NULL));

  path[sizeof(path) - 1] = 0;

  /* Is this fully specified? */

  leaf = strrchr(path, '.');

  if (!leaf)
  {
    StrNCpy0(erb.errmess,
             lookup_token("GivePath:To save, drag the file icon to a directory viewer",
                          0,
                          0));

    erb.errnum = Utils_Error_Custom_Message;

    ChkError(&erb);
  }

  /* Where did we come from? */

  ChkError(toolbox_get_client_handle(0, idb->ancestor_id, (void *) &b));

  if (!is_known_browser(b)) return 0;

  /* First, hide the Save Object dialogue */

  ChkError(saveobject_close(b));

  /* Set the paths etc. and allow the fetch to continue */

  ChkError(save_save_object(path, b));

280 281 282 283 284 285 286 287 288 289 290 291 292
  return 1;
}

/*************************************************/
/* saveobject_cancel()                           */
/*                                               */
/* Handles clicks on the 'Cancel' button in the  */
/* Save Object dialogue                          */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

293
static int saveobject_cancel(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
294
{
295 296 297 298 299 300 301 302 303 304 305
  browser_data * b;

  /* We don't do anything sophisticated, like */
  /* restoring previous options here, as the  */
  /* dialogue is too simple for it to be      */
  /* worthwhile.                              */

  ChkError(toolbox_get_client_handle(0, idb->ancestor_id, (void *) &b));

  /* If we were fetching, stop the fetch */

306
  if (b && is_known_browser(b) && b->save_link) fetch_stop(b, 0);
307 308 309 310 311

  /* Close the dialogue */

  ChkError(saveobject_close(b));

312 313 314 315 316 317 318 319
  return 1;
}

/*************************************************/
/* saveobject_close()                            */
/*                                               */
/* If the Save Object dialogue is opened, this   */
/* will close it, deregistering any associated   */
320
/* event handlers. The dialogue is deleted.      */
321
/*                                               */
322 323
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the dialogue.         */
324 325
/*************************************************/

326
_kernel_oserror * saveobject_close(browser_data * b)
327
{
328
  _kernel_oserror * e;
329
  int               required;
330

331
  if (!b->save_dbox) return NULL;
332

333 334 335 336 337 338 339 340 341 342
  /* Record whatever pathname is in the dialogue for use in */
  /* future dialogues.                                      */

  RetError(writablefield_get_value(0,
                                   b->save_dbox,
                                   SaveObjectWrit,
                                   NULL,
                                   0,
                                   &required));

343 344 345 346 347 348 349 350 351 352
  /* Get rid of any event handlers */

  RetError(event_deregister_toolbox_handlers_for_object(b->save_dbox));

  /* Delete the dialogue */

  e = toolbox_delete_object(0, b->save_dbox);
  b->save_dbox = 0;

  return e;
353
}