Protocols 114 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 25 26
/* 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   : Protocols.c                            */
/*                                                 */
/* Purpose: Handle some common protocol stuff,     */
/*          such as inter-application data         */
/*          transfer.                              */
/*                                                 */
/*          Function naming convention is as usual */
/*          a source leafname derived prefix, then */
/*          a code based on the protocol being     */
/*          dealt with:                            */
/*                                                 */
27 28 29 30 31
/*          _atats_  App To App Transfer, Save     */
/*          _atatl_  App To App Transfer, Load     */
/*          _pp_     Printer Protocol              */
/*          _auh_    Acorn URI Handler             */
/*          _ih_     Interactive Help              */
32
/*          _aub_    ANT URL Broadcast             */
33 34 35 36 37
/*          _multi_  Message is used in more than  */
/*                   one message protocol scheme   */
/*          _util_   A utility function not        */
/*                   directly connected with a     */
/*                   specific protocol.            */
38 39 40 41 42 43 44
/*                                                 */
/*          This is followed by the direction, so  */
/*          to speak - i.e. '_got_' for got a      */
/*          message, '_send_' for sending a        */
/*          message. Alternatively, a prefix       */
/*          '_bounced' is used for messages which  */
/*          return as a UserMessage_Acknowledge    */
45
/*          event.                                 */
46
/*                                                 */
47 48 49 50 51
/*          Because the Plug-In message protocol   */
/*          is such a large and self-contained     */
/*          entity, this is kept separate, in      */
/*          PlugIn.c.                              */
/*                                                 */
52 53 54
/* Author : A.D.Hodgkinson                         */
/*                                                 */
/* History: 29-Aug-97: Created.                    */
55 56 57 58
/*                                                 */
/*          06-Sep-97: Significant rewriting to    */
/*                     stop various clashes and    */
/*                     increase flexibility.       */
59 60 61 62 63 64 65 66 67
/***************************************************/

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

#include "swis.h"
#include "kernel.h"

68
#include "URI.h" /* URI handler API, in URILib:h */
69 70 71 72 73 74 75 76 77

#include "wimp.h"
#include "wimplib.h"
#include "event.h"

#include "toolbox.h"

#include "svcprint.h"
#include "Global.h"
78
#include "MiscDefs.h"
79 80
#include "Utils.h"

81
#include "Browser.h"
82
#include "FetchPage.h"
83
#include "Filetypes.h"
84
#include "History.h"
85
#include "Hotlist.h"
86
#include "Images.h"
87
#include "MimeMap.h"
88 89
#include "Printing.h"
#include "Save.h"
90
#include "SaveDraw.h"
91
#include "SaveFile.h"
92
#include "SaveObject.h"
93
#include "SaveText.h"
94 95 96 97 98 99 100 101 102 103 104 105
#include "Toolbars.h"
#include "URLutils.h"
#include "Windows.h"

#include "Protocols.h"

/* Conditional includes */

#ifdef TRACE
  #include "Trace.h"
#endif

106 107
/* Local definitions */

108
#define RTB_Size 4096 /* RAM transfer buffer size, in bytes */
109

110
/* Local variables */
111

112 113 114 115
/* Need to retain information across function calls about the */
/* different processes going on. Since an object save could   */
/* be in progress whilst other saves go on above it, all of   */
/* the remembered items have to be independent. Consequently, */
116
/* a lot of separate statics are needed.                      */
117

118
/* View source - broadcast loading */
119

120
static int            data_open_reference                = 0;
121

122 123 124 125 126 127
/* Saving the page source */

static int            save_source_reference              = 0;
static int            save_source_transmitted            = 0;
static browser_data * save_source_browser                = NULL;

128 129 130 131 132
/* Exporting the page as text */

static int            save_as_text_reference             = 0;
static browser_data * save_as_text_browser               = NULL;

133 134 135 136 137 138
/* Exporting the page as Draw */

static int            save_as_draw_reference             = 0;
static browser_data * save_as_draw_browser               = NULL;
static int            save_as_draw_backgrounds           = 0;

139 140 141 142 143 144
/* Saving a link */

static int            save_link_reference                = 0;
static int            save_link_transmitted              = 0;
static browser_data * save_link_browser                  = NULL;
static HStream      * save_link_token                    = NULL;
145
static int            save_link_as_url                   = 0;
146 147 148 149 150 151

/* Saving the current location */

static int            save_location_reference            = 0;
static int            save_location_transmitted          = 0;
static browser_data * save_location_browser              = NULL;
152
static int            save_location_as_url               = 0;
153

154
#ifndef REMOTE_HOTLIST
155

156
  /* Saving a hotlist item */
157

158 159 160
  static int            save_hotlist_entry_reference     = 0;
  static int            save_hotlist_entry_transmitted   = 0;
  static hotlist_item * save_hotlist_entry_item          = NULL;
161
  static int            save_hotlist_entry_as_url        = 0;
162

163 164 165 166 167 168 169 170 171 172 173
  /* Saving a hotlist selection */

  static int            save_hotlist_selection_reference   = 0;
  static int            save_hotlist_selection_transmitted = 0;

  /* Saving the entire hotlist */

  static int            save_entire_hotlist_reference      = 0;
  static int            save_entire_hotlist_transmitted    = 0;

#endif
174

175 176 177 178 179
/* Saving the History */

static int            save_history_reference             = 0;
static browser_data * save_history_browser               = NULL;

180 181 182 183 184
/* Saving an object through the fetcher */

static int            save_object_reference              = 0;
static int            save_object_transmitted            = 0;
static browser_data * save_object_browser                = NULL;
185
static int            save_object_through_scrap          = 0;
186 187

/* Exporting an image as a sprite */
188

189 190 191 192 193
static int            save_image_reference               = 0;
static int            save_image_transmitted             = 0;
static browser_data * save_image_browser                 = NULL;
static HStream      * save_image_token                   = NULL;
static int            save_image_as_original             = 0;
194

195 196 197
/* Loading data. For RAM transfer, buffers must be */
/* visible to all functions so they can be freed   */
/* in the event of a transfer failure.             */
198 199 200 201 202

static WimpMessage  * pending_data_save_ack              = NULL;
static char         * data_save_suggested_leafname       = NULL;

static void         * rtb                                = NULL;
203 204 205 206 207
static int            ram_fetch_reference                = 0;

static char         * ram_load_uri_buffer                = NULL;
static int            ram_load_buffer_size               = 0;
static int            ram_load_started                   = 0;
208 209 210 211 212 213 214 215 216

/* Printing */

static int            print_save_reference               = 0;

/* Static function prototypes */

static _kernel_oserror * protocols_atats_got_data_save_ack (WimpMessage * m);
static _kernel_oserror * protocols_pp_got_data_save_ack    (WimpMessage * m);
217

218 219
static void              protocols_util_update_reference   (int old_ref, int new_ref);

220
/*************************************************/
221
/* protocols_atats_send_data_open()              */
222 223 224 225 226 227 228 229 230 231
/*                                               */
/* Broadcasts a Message_DataOpen for the given   */
/* filetype and given pathname.                  */
/*                                               */
/* Parameters: The filetype to use;              */
/*                                               */
/*             Pointer to a null-terminated      */
/*             pathname to use.                  */
/*************************************************/

232
_kernel_oserror * protocols_atats_send_data_open(int filetype, char * pathname)
233
{
234
  WimpMessage dop;
235

236 237 238 239 240 241
  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atats_send_data_open: Called\n");
  #endif

  /* Fill in the header */

242 243 244
  dop.hdr.your_ref    = 0;
  dop.hdr.action_code = Wimp_MDataOpen;

245 246
  /* Fill in the message body */

247 248 249 250 251 252 253 254 255
  dop.data.data_open.window_handle = 0;
  dop.data.data_open.x             = 0;
  dop.data.data_open.y             = 0;
  dop.data.data_open.file_type     = filetype;

  StrNCpy0(dop.data.data_open.path_name, pathname);

  dop.hdr.size = (int) WordAlign(strlen(dop.data.data_open.path_name) + 45); /* (44 for stuff up to the pathname, plus 1 for terminator) */

256 257 258
  /* Send the message */

  RetError(wimp_send_message(Wimp_EUserMessageRecorded, &dop, 0, 0, NULL));
259 260 261

  /* Record my_ref in case it bounces */

262
  data_open_reference = dop.hdr.my_ref;
263

264
  return NULL;
265 266
}

267
/*************************************************/
268
/* protocols_atats_data_open_bounced()           */
269
/*                                               */
270 271 272 273
/* For a View Source function, the browser       */
/* broadcasts a Message_DataOpen. If this        */
/* bounces, attempt to start an editor instead,  */
/* with the pathname specified in the message.   */
274
/*                                               */
275
/* See PRM 3-265.                                */
276 277
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
278
/*             for the received message.         */
279 280
/*************************************************/

281
_kernel_oserror * protocols_atats_data_open_bounced(WimpMessage * m)
282 283
{
  #ifdef TRACE
284
    if (tl & (1u<<28)) Printf("protocols_atats_data_open_bounced: Called\n");
285 286
  #endif

287 288 289 290 291 292 293 294 295 296 297 298 299
  if (m->hdr.my_ref == data_open_reference)
  {
    _kernel_oserror * e;
    char            * combuf;
    char            * comstart = "If \"Alias$@RunType_FFF\" <> \"\" Then @RunType_FFF ";
    int               len;

    data_open_reference = 0;

    /* Want to run '@RunType_FFF' with the given pathname as a parameter;  */
    /* need to assemble this command string. Work out how long it will be. */

    len = strlen(comstart) + strlen(m->data.data_open.path_name) + 1;
300

301
    /* Allocate space for it (complain if this fails). */
302

303
    combuf = malloc(len);
304

305
    if (!combuf) return make_no_memory_error(9);
306

307
    /* Assemble the string */
308

309 310
    strcpy(combuf, comstart);
    strcat(combuf, m->data.data_open.path_name);
311

312
    /* Execute the command */
313

314 315 316 317
    e = _swix(Wimp_StartTask,
              _IN(0),

              combuf);
318

319 320 321 322
    /* Free the command buffer and return the value the _swix call gave back */

    free(combuf);
    combuf = NULL;
323

324
    return e;
325 326
  }

327
  #ifdef TRACE
328

329 330 331
    else
    {
      erb.errnum = Utils_Error_Custom_Normal;
332

333 334 335
      sprintf(erb.errmess,
              "Message reference %x not recognised in protocols_atats_data_open_bounced",
              m->hdr.your_ref);
336

337 338
      return &erb;
    }
339

340
  #endif
341

342 343
  return NULL;
}
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
/*************************************************/
/* protocols_atats_send_data_save()              */
/*                                               */
/* Send out a Message_DataSave to initiate       */
/* saving of data to another applicaion.         */
/*                                               */
/* See PRM 3-250 to 3-252.                       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the save (or NULL);   */
/*                                               */
/*             Pointer to an item that will vary */
/*             depending on what is being saved  */
/*             - e.g. an HStream struct, a       */
/*             hotlist item, or NULL... full     */
/*             details by the definition of the  */
/*             protocols_saving enumeration in   */
/*             Protocols.h;                      */
/*                                               */
/*             Pointer to a null terminated      */
/*             leafname to use;                  */
/*                                               */
/*             If possible, the estimated size   */
/*             of the data, else -1;             */
/*                                               */
/*             Filetype to quote;                */
/*                                               */
/*             A protocols_saving value to say   */
/*             what is being saved (see          */
/*             Protocols.h);                     */
/*                                               */
/*             A WimpGetPointerInfo block        */
/*             pointer, from which the window    */
/*             and icon handle to send to, and   */
/*             x and y coords to send to, are    */
/*             read.                             */
/*************************************************/
382

383 384 385
_kernel_oserror * protocols_atats_send_data_save(browser_data * b, void * extra, char * leaf, int estimated_size, int filetype,
                                                 protocols_saving saving, WimpGetPointerInfoBlock * info)
{
386
  WimpMessage m;
387

388 389 390
  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atats_send_data_save: Called\n");
  #endif
391

392
  /* Fill in the header */
393

394 395 396
  m.hdr.sender      = task_handle;
  m.hdr.your_ref    = 0;
  m.hdr.action_code = Wimp_MDataSave;
397

398
  /* Fill in the message body */
399

400 401 402 403 404 405
  m.data.data_save.destination_window = info->window_handle;
  m.data.data_save.destination_icon   = info->icon_handle;
  m.data.data_save.destination_x      = info->x;
  m.data.data_save.destination_y      = info->y;
  m.data.data_save.estimated_size     = estimated_size;
  m.data.data_save.file_type          = filetype;
406

407
  /* Copy in the leafname and work out the message size */
408

409
  StrNCpy0(m.data.data_save.leaf_name, leaf);
410

411
  m.hdr.size = (int) WordAlign(strlen(m.data.data_save.leaf_name) + 45);
412

413
  /* Send it */
414

415 416 417 418 419
  RetError(wimp_send_message(Wimp_EUserMessage,
                             &m,
                             info->window_handle,
                             info->icon_handle,
                             NULL));
420

421
  /* Remember various details */
422

423 424 425 426 427 428 429 430 431
  switch (saving)
  {
    /* Save the document source */

    case protocols_saving_document_source:
    {
      save_source_reference              = m.hdr.my_ref;
      save_source_transmitted            = 0;
      save_source_browser                = b;
432
    }
433
    break;
434

435 436 437 438 439 440 441 442 443
    /* Export the document as text */

    case protocols_saving_document_as_text:
    {
      save_as_text_reference             = m.hdr.my_ref;
      save_as_text_browser               = b;
    }
    break;

444 445 446 447 448 449 450 451 452 453
    /* Export the document as text */

    case protocols_saving_document_as_draw:
    {
      save_as_draw_reference             = m.hdr.my_ref;
      save_as_draw_browser               = b;
      save_as_draw_backgrounds           = savefile_alternative_selected();
    }
    break;

454
    /* Save a link as a URI file */
455

456 457 458 459 460 461
    case protocols_saving_link:
    {
      save_link_reference                = m.hdr.my_ref;
      save_link_transmitted              = 0;
      save_link_browser                  = b;
      save_link_token                    = (HStream *) extra;
462
      save_link_as_url                   = savefile_alternative_selected();
463 464
    }
    break;
465

466 467 468 469 470 471 472
    /* Save the current location as a URI file */

    case protocols_saving_frame_location:
    {
      save_location_reference            = m.hdr.my_ref;
      save_location_transmitted          = 0;
      save_location_browser              = b;
473
      save_location_as_url               = savefile_alternative_selected();
474 475
    }
    break;
476

477
    #ifndef REMOTE_HOTLIST
478

479
      /* Save a hotlist item as a URI file */
480

481 482 483 484 485
      case protocols_saving_hotlist_entry:
      {
        save_hotlist_entry_reference       = m.hdr.my_ref;
        save_hotlist_entry_transmitted     = 0;
        save_hotlist_entry_item            = (hotlist_item *) extra;
486
        save_hotlist_entry_as_url          = filetype == FileType_URI ? 0 : 1;
487 488
      }
      break;
489

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
      /* Save a selection of hotlist items as an HTML file */

      case protocols_saving_hotlist_selection:
      {
        save_hotlist_selection_reference   = m.hdr.my_ref;
        save_hotlist_selection_transmitted = 0;
      }
      break;

      /* Save the entire hotlist as an HTML file */

      case protocols_saving_entire_hotlist:
      {
        save_entire_hotlist_reference      = m.hdr.my_ref;
        save_entire_hotlist_transmitted    = 0;
      }
      break;

    #endif
509 510 511 512 513 514 515 516

    /* Save an object through the fetcher */

    case protocols_saving_object:
    {
      save_object_reference              = m.hdr.my_ref;
      save_object_transmitted            = 0;
      save_object_browser                = b;
517
      save_object_through_scrap          = 0;
518 519 520 521 522 523 524
    }
    break;

    /* Save an image as a sprite */

    case protocols_saving_image_sprite:
    {
525 526 527 528 529
      save_image_reference               = m.hdr.my_ref;
      save_image_transmitted             = 0;
      save_image_browser                 = b;
      save_image_token                   = (HStream *) extra;
      save_image_as_original             = savefile_alternative_selected();
530
    }
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    break;

    /* Saving the local or global history */

    case protocols_saving_local_history:
    {
      save_history_reference             = m.hdr.my_ref;
      save_history_browser               = b;
    }
    break;

    case protocols_saving_global_history:
    {
      save_history_reference             = m.hdr.my_ref;
      save_history_browser               = NULL;
    }
    break;
548 549
  }

550 551 552
  /* Finished */

  return NULL;
553 554 555
}

/*************************************************/
556
/* protocols_atats_data_save_bounced()           */
557
/*                                               */
558
/* Deals with a Message_DataSave bouncing.       */
559
/*                                               */
560
/* See PRM 3-252.                                */
561 562
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
/*             for the received message.         */
/*************************************************/

_kernel_oserror * protocols_atats_data_save_bounced(WimpMessage * m)
{
  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atats_data_save_bounced: Called\n");
  #endif

  /* We have nothing to do here at present; this */
  /* is only here to be complete.                */

  return NULL;
}

/*************************************************/
/* protocols_atats_got_ram_fetch()               */
/*                                               */
/* After the browser sends a Message_DataSave to */
/* an application, it may reply with             */
/* Message_RAMFetch rather than                  */
/* Message_DataSaveAck if it can handle RAM      */
/* transfer. In that case, we should see if we   */
/* can do RAM transfer for this filetype         */
/* ourselves, and if so, transfer data.          */
/*                                               */
/* See PRM 3-255 and 256.                        */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the Message_RAMFetch. The     */
593 594 595 596
/*             contents will be modified and     */
/*             used in a reply.                  */
/*************************************************/

597
_kernel_oserror * protocols_atats_got_ram_fetch(WimpMessage * m)
598 599
{
  #ifdef TRACE
600
    if (tl & (1u<<28)) Printf("protocols_atats_got_ram_fetch: Called\n");
601 602
  #endif

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  /* Only the transfer methods by which RAM saving is currently */
  /* supported are listed in the switch. For others, we just do */
  /* nothing. The other application will see its message bounce */
  /* and should send Message_DataSaveAck to continue the save   */
  /* through a temporary file on disc. If not, no loss - from   */
  /* the user's perspective, nothing will have happened.        */
  /*                                                            */
  /* Note all the functions called in the switch statement call */
  /* Protocols.c back again with requests to send a reply of    */
  /* Message_RAMTransmit if they're successful.                 */
  /*                                                            */
  /* Stop press. C can't cope - you can't have a non-constant   */
  /* in a case statement. Good grief. So, this nice elegant     */
  /* case statement gets degraded into a grotty if...else       */
  /* ladder. Yuk, yuk, yuk, yuk, yuk. I *hate* this language.   */

619
  if (m->hdr.your_ref && m->hdr.your_ref == save_source_reference)
620
  {
621
    /* Save document source */
622

623 624 625 626 627 628 629
    if (
         is_known_browser(save_source_browser)
       )
       return save_transfer_source(save_source_browser,
                                   &save_source_transmitted,
                                   m);
  }
630
  else if (m->hdr.your_ref && m->hdr.your_ref == save_link_reference)
631
  {
632 633 634 635 636
    if (is_known_browser(save_link_browser))
    {
      if (save_link_token)
      {
        /* Save a link as a URI file */
637

638
        return save_transfer_uri(save_link_token->anchor,
639
                                 NULL,
640
                                 save_link_as_url,
641 642 643 644 645 646 647
                                 &save_link_transmitted,
                                 m);
      }
      else
      {
        /* Save the current location as a URI file */

648 649
        char * url   = browser_current_url  (save_link_browser);
        char * title = browser_current_title(save_link_browser);
650 651 652 653

        if (!url) url = " ";

        return save_transfer_uri(url,
654
                                 title,
655
                                 save_link_as_url,
656 657 658 659
                                 &save_link_transmitted,
                                 m);
      }
    }
660
  }
661
  else if (m->hdr.your_ref && m->hdr.your_ref == save_location_reference)
662 663
  {
    /* Save the current location as a URI file */
664

665 666 667 668 669 670 671 672 673 674
    if (is_known_browser(save_location_browser))
    {
      char * title = browser_current_title(save_location_browser);

      return save_transfer_uri(browser_current_url(save_location_browser),
                               title,
                               save_location_as_url,
                               &save_location_transmitted,
                               m);
    }
675
  }
676

677 678
  #ifndef REMOTE_HOTLIST

679
    else if (m->hdr.your_ref && m->hdr.your_ref == save_hotlist_entry_reference)
680 681 682 683 684 685 686
    {
      /* Save a hotlist item as a URI file */

      if (
           save_hotlist_entry_item
         )
         return save_transfer_uri(save_hotlist_entry_item->data.url,
687
                                  save_hotlist_entry_item->name,
688
                                  save_hotlist_entry_as_url,
689 690 691 692 693
                                  &save_hotlist_entry_transmitted,
                                  m);
    }

  #endif
694

695 696
  return NULL;
}
697

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
/*************************************************/
/* protocols_atats_send_ram_transmit()           */
/*                                               */
/* Send out a Message_RAMTransmit in response to */
/* a Message_RAMFetch from another application,  */
/* as part of an ongoing dialogue for RAM        */
/* transfer.                                     */
/*                                               */
/* See PRM 3-255 and 256.                        */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the Message_RAMFetch. The     */
/*             contents will be modified and     */
/*             used in a reply;                  */
/*                                               */
/*             The number of bytes written to    */
/*             the buffer given in the           */
/*             Message_RAMFetch;                 */
/*                                               */
/*             1 if this is the last message in  */
/*             the transfer (so it won't be sent */
/*             UserMessageRecorded and raise an  */
/*             error when it doesn't get         */
/*             acknowledged...).                 */
/*************************************************/
723

724 725
_kernel_oserror * protocols_atats_send_ram_transmit(WimpMessage * m, int transmitted, int last)
{
726
  int old_ref = m->hdr.your_ref;
727

728 729 730
  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atats_send_ram_transmit: Called\n");
  #endif
731

732
  /* Fill in the parts that need changing */
733

734 735
  m->hdr.your_ref             = m->hdr.my_ref;
  m->hdr.action_code          = Wimp_MRAMTransmit;
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757

  m->data.ram_transmit.nbytes = transmitted;

  #ifdef TRACE

    /* Show the contents of the transmission buffer */

    if (tl & (1u<<28))
    {
      void * local = malloc(m->data.ram_transmit.nbytes);

      if (local)
      {
        if (
             !wimp_transfer_block(m->hdr.sender,
                                  m->data.ram_transmit.buffer,
                                  task_handle,
                                  local,
                                  m->data.ram_transmit.nbytes)
           )
        {
          trace_dump_buffer(m->data.ram_transmit.buffer,
758 759
                            m->data.ram_transmit.nbytes,
                            1);
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
        }
        else Printf("\n(Cannot dump transmission buffer - error from Wimp_TransferBlock)\n\n");
      }
      else
      {
        Printf("\n(Cannot dump transmission buffer - not enough memory for local copy)\n\n");
      }
    }

  #endif

  /* Send the reply */

  RetError(wimp_send_message(last ? Wimp_EUserMessage : Wimp_EUserMessageRecorded,
                             m,
                             m->hdr.sender,
                             0,
                             NULL));

  /* Update the records of my_ref for subsequent replies and bounces */

  protocols_util_update_reference(old_ref, m->hdr.my_ref);

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
  /* If this is the last item, may need to do some tidying up */
  /* (don't do this before sending the message, as here we    */
  /* close the menu tree - if the window we were sending to   */
  /* was in a menu... Well, things Go Wrong.                  */

  if (last)
  {
    #ifndef REMOTE_HOTLIST
      if (m->hdr.my_ref == save_hotlist_entry_reference) RetError(hotlist_clear_selection());
    #endif

    if (
         m->hdr.my_ref == save_source_reference        ||
         m->hdr.my_ref == save_link_reference          ||
         m->hdr.my_ref == save_location_reference

         #ifndef REMOTE_HOTLIST
           || m->hdr.my_ref == save_hotlist_entry_reference
         #endif
       )
    {
      _swix(Wimp_CreateMenu,
            _IN(1),

            -1);
    }
  }

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
  /* Finished */

  return NULL;
}

/*************************************************/
/* protocols_atats_ram_transmit_bounced()        */
/*                                               */
/* If a RAMTransmit is not acknowledged, we      */
/* should abort file transfer and raise an       */
/* error.                                        */
/*                                               */
/* See PRM 3-255 and 256.                        */
/*                                               */
/* Parameters Pointer to the WimpMessage struct  */
/*            for the received message.          */
/*************************************************/

_kernel_oserror * protocols_atats_ram_transmit_bounced(WimpMessage * m)
{
  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atats_ram_transmit_bounced: Called\n");
  #endif

  /* There's no tidying up to do; just raise an error. */

  erb.errnum = Utils_Error_Custom_Message;

  StrNCpy0(erb.errmess,
           lookup_token("RecvDied:Data transfer failed - receiver died.",
                        0,
                        0));

  return &erb;
}

/*************************************************/
/* protocols_atats_got_data_save_ack()           */
/*                                               */
/* Once we've sent out Message_DataSave we could */
/* get Message_RAMFetch back and proceed with    */
/* RAM transfer. Or the other task could respond */
/* with a Message_DataSaveAck, for transfer via  */
/* a temporary file on disc. This can also       */
/* happen if the other application responds with */
/* a Message_RAMFetch but we decide we can't do  */
/* RAM transfer for that particular object;      */
/* the application sees its Message_RAMFetch     */
/* bounce and drops back to Message_DataSaveAck  */
/* instead.                                      */
/*                                               */
/* See PRM 3-250, 251 and 253.                   */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message. The     */
/*             contents will be modified and     */
/*             used in a reply.                  */
/*************************************************/

static _kernel_oserror * protocols_atats_got_data_save_ack(WimpMessage * m)
{
  _kernel_oserror * e    = NULL;
873
  int               ok   = 0; /* 0 = problem, 1 = OK, send DataLoad, 2 = OK, but don't send DataLoad */
874 875 876 877 878 879 880 881 882
  char            * path = m->data.data_save_ack.leaf_name; /* (Daft structure definition - it's a path, not a leaf...) */

  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atats_got_data_save_ack: Called\n");
  #endif

  /* First, save the file. Yes, it's another nasty if...else ladder */
  /* because C can't handle it as a switch statement...             */

883
  if (m->hdr.your_ref && m->hdr.your_ref == save_source_reference)
884 885 886 887 888 889 890 891 892
  {
    /* Save the document source */

    if (is_known_browser(save_source_browser))
    {
      e = save_save_source(path, save_source_browser);
      if (!e) ok = 1;
    }
  }
893 894 895 896 897 898 899 900 901 902
  else if (m->hdr.your_ref && m->hdr.your_ref == save_as_text_reference)
  {
    /* Export the document as text */

    if (is_known_browser(save_as_text_browser))
    {
      e = savetext_save_text(save_as_text_browser, path);
      if (!e) ok = 1;
    }
  }
903 904 905 906 907 908 909 910 911 912
  else if (m->hdr.your_ref && m->hdr.your_ref == save_as_draw_reference)
  {
    /* Export the document as Draw */

    if (is_known_browser(save_as_draw_browser))
    {
      e = savedraw_save_draw(save_as_draw_browser, path, save_as_draw_backgrounds);
      if (!e) ok = 1;
    }
  }
913
  else if (m->hdr.your_ref && m->hdr.your_ref == save_link_reference)
914
  {
915
    if (is_known_browser(save_link_browser))
916
    {
917 918 919 920
      if (save_link_token)
      {
        /* Save a link as a URI file */

921
        e = save_save_uri(path, save_link_token->anchor, NULL, save_link_as_url);
922 923 924 925
        if (!e) ok = 1;
      }
      else
      {
926 927
        char * url   = browser_current_url  (save_link_browser);
        char * title = browser_current_title(save_link_browser);
928

929
        /* Save the current location as a URI file */
930 931 932

        if (!url) url = " ";

933
        e = save_save_uri(path, url, title, 0);
934
        if (!e) ok = 1;
935
      }
936 937
    }
  }
938
  else if (m->hdr.your_ref && m->hdr.your_ref == save_location_reference)
939 940 941 942 943
  {
    /* Save the current location as a URI file */

    if (is_known_browser(save_location_browser))
    {
944 945 946
      char * title = browser_current_title(save_location_browser);

      e = save_save_uri(path, browser_current_url(save_location_browser), title, save_location_as_url);
947 948 949 950
      if (!e) ok = 1;
    }
  }

951 952
  #ifndef REMOTE_HOTLIST

953
    else if (m->hdr.your_ref && m->hdr.your_ref == save_hotlist_entry_reference)
954
    {
955
      /* Save a hotlist item as a URI file */
956

957
      if (save_hotlist_entry_item)
958
      {
959
        e = save_save_uri(path, save_hotlist_entry_item->data.url, save_hotlist_entry_item->name, save_hotlist_entry_as_url);
960 961 962 963 964 965

        if (!e)
        {
          ok = 1;
          e = hotlist_clear_selection();
        }
966 967
      }
    }
968
    else if (m->hdr.your_ref && m->hdr.your_ref == save_hotlist_selection_reference)
969 970
    {
      /* Save a hotlist selection as an HTML file */
971

972
      e = hotlist_save_hotlist(path, NULL, 1);
973

974 975 976 977 978 979
      if (!e)
      {
        ok = 1;
        e = hotlist_clear_selection();
      }
    }
980
    else if (m->hdr.your_ref && m->hdr.your_ref == save_entire_hotlist_reference)
981
    {
982 983
      /* Save the whole hotlist */

984
      e = hotlist_save_hotlist(path, NULL, 0);
985
      if (!e) ok = 1;
986
    }
987 988 989

  #endif

990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
  else if (m->hdr.your_ref && m->hdr.your_ref == save_history_reference)
  {
    /* Save the local or global history */

    if (is_known_browser(save_history_browser))
    {
      e = history_save_as_html(path, save_history_browser);
      if (!e) ok = 1;
    }
    else
    {
      e = history_save_as_html(path, NULL);
      if (!e) ok = 1;
    }
  }
1005
  else if (m->hdr.your_ref && m->hdr.your_ref == save_object_reference)
1006 1007 1008
  {
    if (is_known_browser(save_object_browser))
    {
1009
      /* First, hide the Save Object dialogue */
1010

1011 1012 1013
      e = saveobject_close(save_object_browser);

      if (!e)
1014
      {
1015
        /* If this is through scrap, use a different filename */
1016

1017 1018 1019
        if (m->data.data_save_ack.estimated_size == -1)
        {
          /* Need to store the Message_DataLoad for later */
1020

1021 1022
          save_object_browser->pending_data_load = malloc(sizeof(WimpMessage));
          if (!save_object_browser->pending_data_load) return make_no_memory_error(13);
1023

1024 1025 1026 1027
          #ifdef TRACE
            malloccount += sizeof(WimpMessage);
            if (tl & (1u<<13)) Printf("** malloccount (protocols_atats_got_data_save_ack): \0211%d\0217\n",malloccount);
          #endif
1028

1029
          *save_object_browser->pending_data_load = *m;
1030

1031 1032
          /* Change the filename to something unique and */
          /* update the message length                   */
1033

1034 1035
          protocols_util_make_unique_name(save_object_browser->pending_data_load->data.data_load.leaf_name,
                                          sizeof(save_object_browser->pending_data_load->data.data_load.leaf_name));
1036

1037
          save_object_browser->pending_data_load->hdr.size = (int) WordAlign(strlen(save_object_browser->pending_data_load->data.data_load.leaf_name) + 45);
1038

1039 1040 1041 1042 1043
          /* Exit through the save routine */

          return save_save_object(save_object_browser->pending_data_load->data.data_load.leaf_name,
                                  save_object_browser);
        }
1044

1045
        /* Otherwise, save as normal */
1046

1047 1048 1049 1050 1051
        else
        {
          e = save_save_object(path, save_object_browser);
          if (!e) ok = 1;
        }
1052
      }
1053 1054
    }
  }
1055
  else if (m->hdr.your_ref && m->hdr.your_ref == save_image_reference)
1056
  {
1057
    if (is_known_browser(save_image_browser))
1058
    {
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
      if (!save_image_as_original)
      {
        /* Save as a sprite */

        e = image_export_sprite(path, save_image_browser, save_image_token);
        if (!e) ok = 1;
      }
      else
      {
        /* Save original image - similar to Save Object code. So, */
        /* if this is through scrap, use a different filename.    */

        if (m->data.data_save_ack.estimated_size == -1)
        {
          WimpMessage * pending;

          /* Need to store the Message_DataLoad for later */

          pending = malloc(sizeof(WimpMessage));
          if (!pending) return make_no_memory_error(13);

          #ifdef TRACE
            malloccount += sizeof(WimpMessage);
            if (tl & (1u<<13)) Printf("** malloccount (protocols_atats_got_data_save_ack): \0211%d\0217\n",malloccount);
          #endif

          *pending = *m;

          /* Change the filename to something unique and */
          /* update the message length                   */

          protocols_util_make_unique_name(pending->data.data_load.leaf_name,
                                          sizeof(pending->data.data_load.leaf_name));

          pending->hdr.size = (int) WordAlign(strlen(pending->data.data_load.leaf_name) + 45);

          /* Exit through the save routine */

          e = image_export_original(pending->data.data_load.leaf_name,
                                    save_image_browser,
                                    save_image_token);

          if (!e)
          {
            save_image_browser                    = last_browser;
            save_image_browser->pending_data_load = pending;
            ok                                    = 2;
          }
        }

        /* Otherwise, save as normal */

        else
        {
          e = image_export_original(path,
                                    save_image_browser,
                                    save_image_token);

          if (!e)
          {
            save_image_browser = last_browser;
            ok                 = 2;
          }
        }
      }
1124 1125 1126 1127 1128 1129 1130
    }
  }

  /* If everything is OK, send out a Message_DataLoad in reply */

  if (ok && !e)
  {
1131
    if (ok == 1) e = protocols_atats_send_data_load(m);
1132 1133 1134 1135 1136

    /* Don't close the menu before sending the message, as if the item we are */
    /* sending to is within a menu tree, an Illegal Window Handle error may   */
    /* be raised.                                                             */

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
    _swix(Wimp_CreateMenu,
          _IN(1),

          -1);
  }

  /* Finished */

  return e;
}

/*************************************************/
/* protocols_atats_send_data_load()              */
/*                                               */
/* Send out a Message_DataLoad in response to a  */
/* Message_DataSaveAck from another application, */
/* as part of an ongoing dialogue for file       */
/* transfer.                                     */
/*                                               */
/* See PRM 3-251 and 253.                        */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the Message_DataSaveAck. The  */
/*             contents will be modified and     */
/*             used in the reply.                */
/*************************************************/

_kernel_oserror * protocols_atats_send_data_load(WimpMessage * m)
{
1166 1167
  int old_ref = m->hdr.your_ref;
  int found   = 0;
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239

  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atats_send_data_load: Called\n");
  #endif

  /* Fill in the parts that need changing */

  m->hdr.your_ref    = m->hdr.my_ref;
  m->hdr.action_code = Wimp_MDataLoad;

  /* Find out the file size and type */

  RetError(_swix(OS_File,
                 _INR(0,1) | _OUT(0) | _OUT(4) | _OUT(6),

                 23, /* Read catalogue info for named, stamped object */
                 m->data.data_save_ack.leaf_name,

                 &found,
                 &m->data.data_load.estimated_size,
                 &m->data.data_load.file_type));

  if (found != 1)
  {
    /* Object not found - woo, weird (but it happens!) */

    #ifdef TRACE
      {
        erb.errnum = Utils_Error_Custom_Message;

        StrNCpy0(erb.errmess,
                 "Can't find the file I'm supposed to tell someone else to load in protocols_atats_send_data_load");

        show_error_ret(&erb);
      }
    #endif

    return NULL;
  }

  /* Send the reply */

  RetError(wimp_send_message(Wimp_EUserMessageRecorded,
                             m,
                             m->data.data_save_ack.destination_window,
                             m->data.data_save_ack.destination_icon,
                             NULL));

  /* Update the records of my_ref for subsequent replies and bounces */

  protocols_util_update_reference(old_ref, m->hdr.my_ref);

  /* Finished */

  return NULL;
}

/*************************************************/
/* protocols_atats_data_load_bounced()           */
/*                                               */
/* Called if a Message_DataLoad bounces. Need to */
/* delete any scrap files written and report an  */
/* appropriate error.                            */
/*                                               */
/* See PRM 3-253, 254.                           */
/*                                               */
/* Parameters Pointer to the WimpMessage struct  */
/*            for the received message.          */
/*************************************************/

_kernel_oserror * protocols_atats_data_load_bounced(WimpMessage * m)
{
1240 1241
  int scrap_was_opened = 0;

1242 1243 1244 1245 1246 1247 1248 1249
  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atats_data_load_bounced: Called\n");
  #endif

  /* Do we recognise this message? If so, deal with it, otherwise */
  /* ignore it (or raise a warning in TRACE builds).              */

  if (
1250
       m->hdr.my_ref == save_source_reference              ||
1251
       m->hdr.my_ref == save_as_text_reference             ||
1252
       m->hdr.my_ref == save_as_draw_reference             ||
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
       m->hdr.my_ref == save_link_reference                ||
       m->hdr.my_ref == save_location_reference            ||

       #ifndef REMOTE_HOTLIST

         m->hdr.my_ref == save_hotlist_entry_reference     ||
         m->hdr.my_ref == save_hotlist_selection_reference ||
         m->hdr.my_ref == save_entire_hotlist_reference    ||

       #endif

       m->hdr.my_ref == save_object_reference              ||
1265
       m->hdr.my_ref == save_image_reference               ||
1266
       m->hdr.my_ref == save_history_reference
1267 1268 1269 1270
     )
  {
    /* Not interested in any errors here - just get rid of scrap if we can */

1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
    if (
         !strcmp(m->data.data_load.leaf_name,
                 Save_ScrapFile)
       )
    {
      /* Special case - if saving an object (Shift+Click on a link */
      /* or whatever) through scrap, so the send of a pending      */
      /* Message_DataLoad led to this bounce, don't delete the     */
      /* scrap file as it's the only copy of the data that we      */
      /* have. Instead, open the scrap directory. Otherwise, do as */
      /* PRM 3-254 and delete the scrap file.                      */

      if (!save_object_through_scrap) remove (m->data.data_load.leaf_name);
      else
      {
        char * combuf;
        int    comlen;
        char   new_name[Limits_OS_Pathname];

        save_object_through_scrap = 0;

        /* The first thing we need to do is change the filename away */
        /* from Scrap, or it could get overwritten when someone else */
        /* does a transfer.                                          */

        protocols_util_make_unique_name(new_name, sizeof(new_name));

        comlen = strlen("Rename ")                   +
                 strlen(m->data.data_load.leaf_name) +
                 1                                   + /* Space between the two names */
                 strlen(new_name)                    +
                 1;                                    /* Terminating byte */

        /* Allocate space for the Rename command; if it fails, fine */
        /* - just don't rename.                                     */

        combuf = malloc(comlen);

        if (combuf)
        {
          sprintf(combuf,
                  "%s%s%c%s",
                  "Rename ",
                  m->data.data_load.leaf_name,
                  ' ',
                  new_name);

          /* Execute the command */

          _swix(Wimp_StartTask,
                _IN(0),
                combuf);

          /* Free the buffer */

          free(combuf);
          combuf = NULL;
        }

        /* Try and allocate a buffer for the Filer_OpenDir command. */
        /* If this fails, let it do so silently.                    */

        comlen = strlen("Filer_OpenDir ") + strlen(Save_ScrapDir) + 1;
        combuf = malloc(comlen);

        if (combuf)
        {
          /* Copy in the command */

          strcpy(combuf, "Filer_OpenDir ");
          strcat(combuf, Save_ScrapDir);

          /* Execute it - if there was no error, flag that Scrap was opened */

          if (
               !_swix(Wimp_StartTask,
                      _IN(0),

                      combuf)
             )
             scrap_was_opened = 1;

          /* Free the temporary buffer */

          free(combuf);
        }
      }
    }
1359 1360 1361 1362 1363

    /* Report an appropriate message */

    erb.errnum = Utils_Error_Custom_Message;

1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378

    if (!scrap_was_opened)
    {
      StrNCpy0(erb.errmess,
               lookup_token("RecvDied:Data transfer failed - receiver died.",
                            0,
                            0));
    }
    else
    {
      StrNCpy0(erb.errmess,
               lookup_token("RecvDiedKept:Data transfer failed - receiver died. The temporary file has been kept on disc so the fetched data can be recovered.",
                            0,
                            0));
    }
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426

    return &erb;
  }

  #ifdef TRACE

    /* In TRACE builds, raise a warning if we don't recognise the message */

    else
    {
      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Message reference %x not recognised in protocols_atats_data_load_bounced",
              m->hdr.your_ref);

      return &erb;
    }

  #endif

  return NULL;
}

/*************************************************/
/* protocols_atats_got_data_load_ack()           */
/*                                               */
/* Deals with a Message_DataLoadAck - completion */
/* of app to app transfer from the browser.      */
/*                                               */
/* See PRM 3-250, 251, 254.                      */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message.         */
/*************************************************/

_kernel_oserror * protocols_atats_got_data_load_ack(WimpMessage * m)
{
  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atats_got_data_load_ack: Called\n");
  #endif

  /* We have nothing to do here at present; this */
  /* is only here to be complete.                */

  return NULL;
}

1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
/*************************************************/
/* protocols_atats_send_any_pendings()           */
/*                                               */
/* Sends any pending messages related to the     */
/* given browser, in the context of application  */
/* to application saving.                        */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the pending messages, */
/*             if any.                           */
/*************************************************/

_kernel_oserror * protocols_atats_send_any_pendings(browser_data * b)
{
  _kernel_oserror * e = NULL;

  if (!b || !is_known_browser(b)) return NULL;

  /* Pending Message_DataLoad - an object has been saved */
  /* through the fetcher to a temporary file.            */

  if (b->pending_data_load)
  {
    char * combuf;
    int    comlen;
    int    type;

    b->pending_data_load->hdr.my_ref = 0;

    /* Update the other state variables */

    save_object_reference     = b->pending_data_load->hdr.your_ref;
    save_object_transmitted   = 0;
    save_object_browser       = b;
    save_object_through_scrap = 1;

    /* Delete scrap if present, rename the file we've written to */
    /* ScrapFile, and alter the message back to using this       */
    /* filename.                                                 */

    remove(Save_ScrapFile);

    /* Does Scrap still exist? The above may have failed because */
    /* Save_ScrapFile was in use elsewhere. If so, then leave    */
    /* the filename as-is.                                       */

    type = 1;

    _swix(OS_File,
          _INR(0,1) | _OUT(0),

          17, /* Read catalogue info for object */
          Save_ScrapFile,

          &type);

    if (!type)
    {
      /* Allocate space for the buffer */

      comlen = strlen("Rename ")                                      +
               strlen(b->pending_data_load->data.data_load.leaf_name) +
               1                                                      + /* The space between the two names */
               strlen(Save_ScrapFile)                                 +
               1;                                                       /* Terminating byte */

      combuf = malloc(comlen);

      /* If we can't allocate space, as with the case of the scrap */
      /* file being undeletable, stick with the existing filename. */

      if (combuf)
      {
        /* We can allocate space, so build the command string. */

        sprintf(combuf,
                "%s%s%c%s",
                "Rename ",
                b->pending_data_load->data.data_load.leaf_name,
                ' ',
                Save_ScrapFile);

        /* Do it. If the command fails, use the original filename. */

        if (
             !_swix(Wimp_StartTask,
                    _IN(0),

                    combuf)
           )
        {
          /* The command worked, so change the message appropriately. */

          StrNCpy0(b->pending_data_load->data.data_load.leaf_name, Save_ScrapFile);

          b->pending_data_load->hdr.size = (int) WordAlign(strlen(Save_ScrapFile) + 45);
        }

        /* Free the command buffer */

        free(combuf);
      }
    }

    /* At last, send the message */

    e = protocols_atats_send_data_load(b->pending_data_load);

    /* We don't need the message block in the browser_data structure */
    /* any more, so free it.                                         */

    free(b->pending_data_load);
    b->pending_data_load = NULL;
  }

  return e;
}

1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
/*************************************************/
/* protocols_atatl_got_data_open()               */
/*                                               */
/* Handle reception of a Message_DataOpen - we   */
/* may want to load a given file. It is an       */
/* application to application related the        */
/* consideration as we are transfering data      */
/* from the Filer, in a sense.                   */
/*                                               */
/* See PRM 3-265.                                */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message. The     */
/*             contents will be modified and     */
/*             used in a reply.                  */
/*************************************************/

_kernel_oserror * protocols_atatl_got_data_open(WimpMessage * m)
{
  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atatl_got_data_open: Called\n");
  #endif

1568 1569 1570 1571
  /* Don't load it if we're the originator */

  if (m->hdr.sender == task_handle) return NULL;

1572 1573 1574 1575
  /* Don't want to load a text or data file from double-clicking, */
  /* only by dragging to a window or the icon bar icon. Similarly */
  /* ANT's URL files should only be loaded if dragged on, and the */
  /* same is true of URI files if we are using the URI handler.   */
1576
  /* Since there is no native TIFF support, drop that out too.    */
1577 1578 1579

  if (
       m->data.data_open.file_type == FileType_TEXT ||
1580 1581
       m->data.data_open.file_type == FileType_DATA ||
       m->data.data_open.file_type == FileType_DOS  || /* (Equivalent to FileType_DATA for PC files, so to speak) */
1582
       m->data.data_open.file_type == FileType_TIFF ||
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
       m->data.data_open.file_type == FileType_URL  ||
       (
         m->data.data_open.file_type == FileType_URI &&
         uri_module_present
       )
     )
     return NULL;

  /* Now treat as a DataLoad message to avoid duplicating load code. */

  m->data.data_load.destination_window = 0; /* Force a new window to open */
  m->data.data_load.destination_icon   = -1;
  m->data.data_load.estimated_size     = 0;

  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atatl_got_data_open: Exitting through protocols_atatl_got_data_load\n");
  #endif

  return protocols_atatl_got_data_load(m);
}

/*************************************************/
/* protocols_atatl_got_data_load()               */
/*                                               */
/* Deals with a Message_DataLoad - if we can     */
/* handle the file, load it.                     */
/*                                               */
/* See PRM 3-253.                                */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message. The     */
/*             contents will be modified and     */
/*             used in a reply.                  */
/*************************************************/

_kernel_oserror * protocols_atatl_got_data_load(WimpMessage * m)
{
1620 1621
  int         filetype = m->data.data_load.file_type;
  ComponentId c;
1622

1623 1624 1625 1626
  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atatl_got_data_load: Called\n");
  #endif

1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
  /* Was the URL writable targetted? In this case, if it was a text file, */
  /* assume it contained a URL - modify the Wimp Message block to contain */
  /* an ANT URL file filetype.                                            */

  RetError(window_wimp_to_toolbox(0,
                                  m->data.data_save.destination_window,
                                  m->data.data_save.destination_icon,
                                  NULL,
                                  &c));

  if (c == URLBarWrit && filetype == FileType_TEXT) filetype = FileType_URL;

1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
  /* If we've got a Data or DOS filetype, can we get anything */
  /* more from the filename?                                  */

  if (
       filetype == FileType_DATA ||
       filetype == FileType_DOS
     )
  {
    char * ext = strrchr(m->data.data_load.leaf_name, '/'); /* Since under RISC OS, filename extensions will start with '/' */

    if (ext)
    {
      ext++;

      if (mimemap_extension_to_riscos(ext, &filetype)) filetype = FileType_DATA;
    }
  }

1657 1658 1659
  /* Proceed only if it's a filetype we can handle */

  if (
1660 1661 1662 1663 1664 1665 1666 1667
       filetype == FileType_HTML ||
       filetype == FileType_TEXT ||
       filetype == FileType_GIF  ||
       filetype == FileType_JPEG ||
       filetype == FileType_PNG  ||
       filetype == FileType_TIFF ||
       filetype == FileType_URL  ||
       filetype == FileType_URI
1668 1669
     )
  {
1670 1671 1672
    char url  [Limits_URL];
    char title[Limits_Title];

1673 1674 1675
    int  apptoapp = m->hdr.your_ref ? 1 : 0; /* your_ref is zero if from filer, else filled in for app to app */

    #ifdef TRACE
1676
      if (tl & (1u<<28)) Printf("protocols_atatl_got_data_load: Can handle this filetype (0x%03x)\n", filetype);
1677 1678
    #endif

1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
    /* Multiuser builds, if not logged in, just acknowledge */
    /* the message to stop (e.g.) another browser being     */
    /* launched by the Filer, then complain.                */

    #ifndef SINGLE_USER

      #ifdef TRACE
        if (tl & (1u<<28)) Printf("protocols_atatl_got_data_load: Not logged in\n");
      #endif

      if (!logged_in)
      {
        show_error_ret(protocols_atatl_send_data_load_ack(m));

        erb.errnum = Utils_Error_Custom_Message;

        StrNCpy0(erb.errmess,
                 lookup_token("MustLogIn:The browser cannot fetch Web pages until you log in.",
                              0,
                              0));

        show_error_ret(&erb);

        return NULL;
      }

    #endif

1707 1708 1709
    /* Process the file */

    if (
1710 1711
         filetype != FileType_URI &&
         filetype != FileType_URL
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
       )
    {
      /* Not a URI or URL file, so text, HTML, image... */

      StrNCpy0(url, m->data.data_load.leaf_name);
      urlutils_pathname_to_url(url, sizeof(url));

      /* For text and HTML, we can use an internal URL scheme */
      /* to allow the fetcher to load the scrap file and then */
      /* remove it, as it should. For other types, the main   */
      /* fetcher modules must be used; so we can't get rid    */
      /* of the scrap file. If something else trashes it,     */
      /* tough.                                               */

      if (
           apptoapp &&
           (
1729 1730
             filetype == FileType_TEXT ||
             filetype == FileType_HTML
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
           )
         )
      {
        StrNCpy0(url, Internal_URL ForScrapFile ":");

        /* Append the application suggested leafname */

        if (!data_save_suggested_leafname || !*data_save_suggested_leafname)
        {
          if (strlen(url) + 9 <= sizeof(url)) strcat(url, "HTMLfile");
        }
        else
        {
          if (strlen(url) + strlen(data_save_suggested_leafname) + 1 <= sizeof(url)) strcat(url, data_save_suggested_leafname);
        }

        /* Don't need a leafname record now */

        if (data_save_suggested_leafname)
        {
          free(data_save_suggested_leafname);
          data_save_suggested_leafname = NULL;
        }
      }
    }
    else
    {
1758
      urlutils_load_uri_file(url, sizeof(url), title, sizeof(title), m->data.data_load.leaf_name);
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771

      /* Delete scrap if used - we've finished with the */
      /* file now, certainly.                           */

      if (apptoapp) remove(m->data.data_load.leaf_name);
    }

    /* Take appropriate action */

    if (m->data.data_load.destination_window <= 0)
    {
      /* Load file to icon bar - i.e. open a new window. */

1772
      ChkError(windows_create_browser(url, NULL, NULL, NULL, Windows_CreateBrowser_Normal));
1773 1774
    }

1775
    #ifndef REMOTE_HOTLIST
1776

1777
      else
1778
      {
1779
        int handled = 0;
1780

1781 1782
        /* We allow two places for loading - a browser window (or bits of it), */
        /* and for URI / URL files, the hotlist window. Check the latter.      */
1783

1784
        if (
1785 1786
             filetype == FileType_URI ||
             filetype == FileType_URL
1787 1788
           )
        {
1789
          ObjectId this;
1790

1791 1792 1793 1794 1795 1796 1797 1798 1799
          /* If we can get the window object ID... */

          if (
               !window_wimp_to_toolbox(0,
                                       m->data.data_load.destination_window,
                                       -1,
                                       &this,
                                       NULL)
             )
1800
          {
1801 1802 1803 1804 1805
            /* ...and it matches that of the hotlist, add the item. */

            if (this == hotlist_return_window_id())
            {
              char * leaf = NULL;
1806

1807
              /* If we have a title for the URL, use it, else try other methods... */
1808

1809 1810 1811 1812 1813 1814 1815 1816
              if (!*title)
              {
                /* For the description, use the source leafname, unless this */
                /* is from another app (your_ref is non-zero), in which case */
                /* use the URL, as <Wimp$Scrap> isn't very friendly or       */
                /* unique.                                                   */

                if (!m->hdr.your_ref) leaf = strrchr(m->data.data_load.leaf_name, '.');
1817

1818 1819 1820
                if (!leaf) leaf = url;
                else       leaf ++;
              }
1821

1822 1823
              ChkError(hotlist_add_position(m->data.data_load.destination_x,
                                            m->data.data_load.destination_y,
1824
                                            *title ? title : leaf,
1825
                                            url));
1826

1827 1828
              handled = 1;
            }
1829 1830 1831
          }
        }

1832 1833
        /* We can also load HTML files, in a fashion, to the hotlist. */

1834
        if (filetype == FileType_HTML)
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
        {
          ObjectId this;

          /* If we can get the window object ID... */

          if (
               !window_wimp_to_toolbox(0,
                                       m->data.data_load.destination_window,
                                       -1,
                                       &this,
                                       NULL)
             )
          {
            /* ...and it matches that of the hotlist, add the item. */

            if (this == hotlist_return_window_id())
            {
              char * path = m->data.data_load.leaf_name;

              ChkError(hotlist_add_html_file(m->data.data_load.destination_x,
                                             m->data.data_load.destination_y,
                                             path));

              handled = 1;
            }
          }
        }

        /* If not handled yet, deal with the file */

1865 1866 1867
        if (!handled)
        {
          browser_data * b = NULL;
1868

1869 1870
          /* Otherwise, load file to a browser window. Need to find */
          /* its browser_data structure for this.                   */
1871

1872
          utils_browser_from_window(m->data.data_load.destination_window, &b);
1873

1874 1875
          if (b && !b->small_fetch) ChkError(fetchpage_new(b, url, 1, 0));
        }
1876
      }
1877 1878

    #endif
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928

    ChkError(protocols_atatl_send_data_load_ack(m));
  }

  return NULL;
}

/*************************************************/
/* protocols_atatl_send_data_load_ack()          */
/*                                               */
/* In response to a Message_DataLoad, reply with */
/* a Message_DataLoadAck.                        */
/*                                               */
/* See PRM 3-252 and 254.                        */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message. The     */
/*             contents will be modified and     */
/*             used in a reply.                  */
/*************************************************/

_kernel_oserror * protocols_atatl_send_data_load_ack(WimpMessage * m)
{
  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atatl_send_data_load_ack: Called\n");
  #endif

  m->hdr.your_ref    = m->hdr.my_ref;
  m->hdr.action_code = Wimp_MDataLoadAck;

  return wimp_send_message(Wimp_EUserMessage, m, m->hdr.sender, 0, NULL);
}

/*************************************************/
/* protocols_atatl_got_data_save()               */
/*                                               */
/* Handle reception of a Message_DataSave - load */
/* data from another application. Handles RAM    */
/* transfer for some filetypes.                  */
/*                                               */
/* See PRM 3-252, 253, 255 and 256.              */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message. The     */
/*             contents will be modified and     */
/*             used in a reply.                  */
/*************************************************/

_kernel_oserror * protocols_atatl_got_data_save(WimpMessage * m)
{
1929 1930 1931
  _kernel_oserror * e        = NULL;
  int               filetype = m->data.data_save.file_type;
  int               ram      = 0;
1932
  WimpMessage     * reply;
1933
  ComponentId       c;
1934 1935 1936 1937 1938

  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_atatl_got_data_save: Called\n");
  #endif

1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
  /* Was the URL writable targetted? In this case, if it was a text file, */
  /* assume it contained a URL - modify the Wimp Message block to contain */
  /* an ANT URL file filetype.                                            */

  RetError(window_wimp_to_toolbox(0,
                                  m->data.data_save.destination_window,
                                  m->data.data_save.destination_icon,
                                  NULL,
                                  &c));

  if (c == URLBarWrit && filetype == FileType_TEXT) filetype = FileType_URL;

1951 1952 1953
  /* Can we do RAM transfer for this file type? */

  if (
1954 1955
       filetype == FileType_URI ||
       filetype == FileType_URL
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
     )
     ram = 1;

  /* Can't do anything if <Wimp$Scrap> isn't defined and we want to */
  /* use it for file transfer. Note that the checking function will */
  /* raise an appropriate error in passing ('<Wimp$Scrap> not       */
  /* defined', for example).                                        */

  if (!ram && utils_check_scrap()) return NULL;

  /* Free any allocated data left over from previous calls */

  if (pending_data_save_ack)
  {
    #ifdef TRACE
      if (tl & (1u<<28)) Printf("protocols_atatl_got_data_save: Freeing old pending Acknowledge block\n");
    #endif

    free(pending_data_save_ack);
    pending_data_save_ack = NULL;
  }

  if (data_save_suggested_leafname)
  {
    #ifdef TRACE
      if (tl & (1u<<28)) Printf("protocols_atatl_got_data_save: Freeing old suggested leafname block\n");
    #endif

    free(data_save_suggested_leafname);
    data_save_suggested_leafname = NULL;
  }

  /* Record the leafname. Thus, when the Message_DataLoad comes along  */
  /* or the RAM transfer is finished, we can use this recorded leaf in */
  /* some meaningful place (such as the URL bar). Otherwise, we have   */
  /* no record (only e.g. '<Wimp$Scrap>' in a Message_DataLoad) and    */
  /* things get a bit more ugly at the front-end.                      */

  if (*m->data.data_save.leaf_name)
  {
    /* Allocate the block */

    data_save_suggested_leafname = malloc(strlen(m->data.data_save_ack.leaf_name) + 1);

    /* Do nothing if this should fail; assuming we managed  */
    /* to load the file, allow the load routines to default */
    /* down to a general alternative in the absence of this */
    /* record.                                              */

    if (data_save_suggested_leafname) strcpy(data_save_suggested_leafname, m->data.data_save_ack.leaf_name);
  }

  /* Deal with RAM transfer - send out a RAMFetch. If this */
  /* bounces, we'll need to drop back to scrap files.      */

  if (ram)
  {
    /* First, we may need to use file transfer so fill in */
    /* a pending DataSaveAck block for later use.         */

    pending_data_save_ack = malloc(sizeof(WimpMessage));

    if (!pending_data_save_ack) return make_no_memory_error(11);

    #ifdef TRACE
      if (tl & (1u<<28)) Printf("protocols_atatl_got_data_save: Allocated new pending Acknowledge block\n");
    #endif

    reply = pending_data_save_ack;
  }
  else
  {
    /* Otherwise, just point back to the existing message and modify */
    /* its contents for a reply in the normal fashion.               */

    reply = m;
  }

  /* Fill it in */

  *reply = *m;

2038 2039
  reply->hdr.your_ref    = m->hdr.my_ref;
  reply->hdr.action_code = Wimp_MDataSaveAck;
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059

  reply->data.data_save_ack.estimated_size = -1;

  /* Write the filename for Scrap to the message block */
  /* and update the size field in the header.          */

  StrNCpy0(reply->data.data_save_ack.leaf_name, Save_ScrapFile);

  reply->hdr.size = (int) WordAlign(strlen(Save_ScrapFile) + 45); /* (44 for stuff up to the pathname, plus 1 for terminator) */

  /* For RAM transfer, send a Message_RAMFetch */

  if (ram)
  {
    WimpMessage * raf = m;

    /* Free any existing RAM transfer buffer and allocate a new one */

    if (rtb) free(rtb);
    rtb = malloc(RTB_Size);
2060

2061
    /* If the allocation fails, drop through to a scrap file method */
2062

2063 2064 2065
    if (rtb)
    {
      /* Fill in the message details */
2066

2067 2068 2069 2070
      #ifdef TRACE
        malloccount += RTB_Size;
        if (tl & (1u<<13)) Printf("** malloccount (protocols_atatl_got_data_save): \0211%d\0217\n",malloccount);
      #endif
2071

2072 2073 2074
      raf->hdr.size                   = 28;
      raf->hdr.your_ref               = m->hdr.my_ref;
      raf->hdr.action_code            = Wimp_MRAMFetch;
2075

2076 2077
      raf->data.ram_fetch.buffer      = rtb;
      raf->data.ram_fetch.buffer_size = RTB_Size;
2078

2079
      /* Send it */
2080

2081 2082 2083
      #ifdef TRACE
        if (tl & (1u<<28)) Printf("protocols_atatl_got_data_save: Sending Message_RAMFetch in response\n");
      #endif
2084

2085 2086 2087 2088 2089
      RetError(wimp_send_message(Wimp_EUserMessageRecorded,
                                 raf,
                                 m->hdr.sender,
                                 0,
                                 NULL));
2090

2091
      /* Remember my_ref in case it bounces */
2092

2093
      ram_fetch_reference = raf->hdr.my_ref;
2094 2095 2096 2097

      /* Flag that we've not started transfer of actual data yet */

      ram_load_started = 0;
2098
    }
2099 2100
    else ram = 0; /* So that the Message_DataSaveAck goes out instead */
  }
2101

2102
  /* Otherwise send a Message_DataSaveAck reply */
2103

2104 2105 2106 2107 2108 2109 2110
  if (!ram) /* Must use this as the RAM transfer code will set 'ram' to 0 if it can't claim a transfer buffer */
  {
    RetError(wimp_send_message(Wimp_EUserMessage, /* Not interested in it bouncing */
                               reply,
                               m->hdr.sender,
                               0,
                               NULL));
2111 2112
  }

2113
  return e;
2114 2115 2116
}

/*************************************************/
2117
/* protocols_atatl_ram_fetch_bounced()           */
2118
/*                                               */
2119 2120 2121 2122 2123 2124 2125 2126 2127
/* Following getting a Message_DataSave,         */
/* protocols_atatl_got_data_save may send out a  */
/* Message_RAMFetch. If this bounces, this       */
/* function is called. If the bounce is indeed   */
/* due to the RAMFetch we sent, then send out    */
/* a Message_DataSaveAck instead, to try and use */
/* scrap file transfer. This is stored in the    */
/* 'pending' block by the same function that     */
/* sends the Message_RAMFetch.                   */
2128
/*                                               */
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
/* Alternatively, a Message_RAMFetch being sent  */
/* in reply to a Message_RAMTransmit *during*    */
/* RAM transfer could bounce, and this function  */
/* would be called. In that case, we should      */
/* abort data transfer. The other end is meant   */
/* to raise an error (which is odd, since it     */
/* seems likely that it will have died - since   */
/* it's sending, the changes of running out of   */
/* memory at that end are slim).                 */
/*                                               */
2139
/* See PRM 3-255.                                */
2140 2141
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
2142
/*             for the received message.         */
2143 2144
/*************************************************/

2145
_kernel_oserror * protocols_atatl_ram_fetch_bounced(WimpMessage * m)
2146 2147
{
  #ifdef TRACE
2148
    if (tl & (1u<<28)) Printf("protocols_atatl_ram_fetch_bounced: Called\n");
2149 2150
  #endif

2151
  if (m->hdr.my_ref == ram_fetch_reference)
2152
  {
2153 2154 2155 2156
    if (!ram_load_started && pending_data_save_ack)
    {
      /* The bounce is from the first Message_RAMFetch, so try using */
      /* scrap file transfer instead.                                */
2157

2158
      _kernel_oserror * e;
2159

2160 2161 2162
      #ifdef TRACE
        if (tl & (1u<<28)) Printf("protocols_atatl_ram_fetch_bounced: Recognised bounce for a new transfer - using Scrap instead\n");
      #endif
2163

2164 2165
      /* Before doing this, make sure Scrap is present - the call */
      /* will raise an error if not, before returning here        */
2166

2167
      if (utils_check_scrap()) return NULL;
2168

2169
      /* Send the pending message */
2170

2171 2172 2173 2174 2175
      e = wimp_send_message(Wimp_EUserMessage,
                            pending_data_save_ack,
                            pending_data_save_ack->hdr.sender,
                            0,
                            NULL);
2176

2177
      /* Free the block and return any error that may have been generated */
2178

2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
      free(pending_data_save_ack);
      pending_data_save_ack = NULL;

      return e;
    }
    else
    {
      #ifdef TRACE
        if (tl & (1u<<28)) Printf("protocols_atatl_ram_fetch_bounced: Recognised bounce for an ongoing transfer - aborting\n");
      #endif

      /* The bounce is from an ongoing RAM transfer, so abort that */
      /* transfer - free any in-use buffers.                       */

      if (ram_load_uri_buffer)
      {
        free(ram_load_uri_buffer);
        ram_load_uri_buffer = NULL;

        #ifdef TRACE
          malloccount -= ram_load_buffer_size;
          if (tl & (1u<<13)) Printf("** malloccount (protocols_atatl_ram_fetch_bounced): \0212%d\0217\n",malloccount);
        #endif
      }

      ram_load_buffer_size = 0;

      if (rtb)
      {
        free(rtb);
        rtb = NULL;

        #ifdef TRACE
          malloccount -= RTB_Size;
          if (tl & (1u<<13)) Printf("** malloccount (protocols_atatl_ram_fetch_bounced): \0212%d\0217\n",malloccount);
        #endif
      }

      /* Free the pending Message_DataSaveAck block, if present */

      if (pending_data_save_ack)
      {
        free(pending_data_save_ack);
        pending_data_save_ack = 0;
      }
    }
2225 2226 2227
  }

  return NULL;
2228 2229 2230
}

/*************************************************/
2231
/* protocols_atatl_got_ram_transmit()            */
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
/*                                               */
/* Deals with a Message_RAMTransmit - we have    */
/* some data in the 'rtb' buffer.                */
/*                                               */
/* See PRM 3-255, 256.                           */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message. The     */
/*             contents will be modified and     */
/*             used in a reply.                  */
/*************************************************/

2244
_kernel_oserror * protocols_atatl_got_ram_transmit(WimpMessage * m)
2245 2246 2247 2248 2249 2250
{
  _kernel_oserror * e        = NULL;
  int               ok       = 1;
  int               finished = 0;

  #ifdef TRACE
2251
    if (tl & (1u<<28)) Printf("protocols_atatl_got_ram_transmit: Called with %d bytes to receive\n", m->data.ram_transmit.nbytes);
2252 2253
  #endif

2254 2255 2256 2257 2258
  /* Flag that transfer is in progress */

  ram_load_started = 1;

  /* Flag if we've finished, too */
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271

  if (m->data.ram_transmit.nbytes < RTB_Size) finished = 1;
  else if (m->data.ram_transmit.nbytes > RTB_Size) m->data.ram_transmit.nbytes = RTB_Size; /* You *never* know...! */

  /* We should know where the buffer is */

  #ifdef TRACE

    if (m->data.ram_transmit.buffer != rtb)
    {
      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
2272
              "Expected to get buffer %p but got %p instead, in protocols_atatl_got_ram_transmit",
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
              rtb,
              m->data.ram_transmit.buffer);

      e  = &erb;
      ok = 0;
    }

  #else

    if (m->data.ram_transmit.buffer != rtb) ok = 0;

  #endif

  /* The action here depends on the type of file being transferred; */
  /* we know from here on in that the 'rtb' buffer is the buffer    */
  /* that has been filled in (see above), so we can use this rather */
  /* than the more cumbersome 'm->data.ram_transmit.buffer'.        */

2291
  if (ok && pending_data_save_ack)
2292
  {
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307
    ComponentId c;
    int         filetype = pending_data_save_ack->data.data_save_ack.file_type;

    /* Was this dragged to the URL writable, and thus should be */
    /* treated as an ANT URL file (if text)?                    */

    RetError(window_wimp_to_toolbox(0,
                                    pending_data_save_ack->data.data_save_ack.destination_window,
                                    pending_data_save_ack->data.data_save_ack.destination_icon,
                                    NULL,
                                    &c));

    if (c == URLBarWrit && filetype == FileType_TEXT) filetype = FileType_URL;

    switch (filetype)
2308 2309 2310 2311 2312 2313 2314 2315 2316
    {
      /* For URI / URL files, copy data from the file transfer */
      /* buffer (which may deliver the file in several small   */
      /* chunks) to a cumulative file buffer, and then process */
      /* that whole thing when finished.                       */

      case FileType_URL: /* Same as for URI files, so no 'break' */
      case FileType_URI:
      {
2317
        int first = 0;
2318

2319 2320 2321 2322 2323
        /* Only proceed with transfer if there are some bytes to get  */
        /* (otherwise, this was the last message with the final chunk */
        /* of data having exactly filled the buffer last time - so    */
        /* act upon the buffer contents now, without trying to        */
        /* transfer zero bytes first).                                */
2324 2325 2326 2327 2328

        if (m->data.ram_transmit.nbytes)
        {
          /* Allocate or extend the local buffer to take in the URI file */

2329
          if (!ram_load_uri_buffer)
2330 2331 2332 2333 2334 2335 2336 2337
          {
            /* Note we allocate 1 byte more than needed to allow a terminator to */
            /* be inserted after the URL string when the transfer has ended;     */
            /* this is because a URI file may end simply by EOF, it does not     */
            /* need a terminator (and in any case, that terminator may be a      */
            /* control char such as ASCII 10 or 13, which C would not treat as a */
            /* string terminator).                                               */

2338 2339
            ram_load_uri_buffer = malloc(m->data.ram_transmit.nbytes + 1);
            first               = 1;
2340 2341 2342

            /* Complain if the allocation fails */

2343
            if (!ram_load_uri_buffer)
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
            {
              e = make_no_memory_error(7);

              ok = 0;
            }

            #ifdef TRACE

              else
              {
                malloccount += m->data.ram_transmit.nbytes + 1;
2355
                if (tl & (1u<<13)) Printf("** malloccount (protocols_atatl_got_ram_transmit): \0211%d\0217\n",malloccount);
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
              }

            #endif
          }
          else
          {
            void * local;

            /* realloc to a new size */

2366
            local = realloc(ram_load_uri_buffer, ram_load_buffer_size + m->data.ram_transmit.nbytes);
2367 2368 2369 2370 2371

            if (!local)
            {
              /* If the allocation fails, free the whole thing */

2372 2373
              free(ram_load_uri_buffer);
              ram_load_uri_buffer = NULL;
2374 2375

              #ifdef TRACE
2376 2377
                malloccount -= ram_load_buffer_size;
                if (tl & (1u<<13)) Printf("** malloccount (protocols_atatl_got_ram_transmit): \0212%d\0217\n",malloccount);
2378 2379
              #endif

2380
              ram_load_buffer_size = 0;
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390

              e = make_no_memory_error(7);

              ok = 0;
            }

            /* Otherwise record a possibly new buffer address */

            else
            {
2391
              ram_load_uri_buffer = local;
2392 2393 2394

              #ifdef TRACE
                malloccount += m->data.ram_transmit.nbytes;
2395
                if (tl & (1u<<13)) Printf("** malloccount (protocols_atatl_got_ram_transmit): \0211%d\0217\n",malloccount);
2396 2397 2398 2399 2400 2401 2402 2403 2404
              #endif
            }
          }

          if (ok)
          {
            /* Copy the data from the file transfer buffer into */
            /* the cumulative URI file buffer                   */

2405
            memcpy(ram_load_uri_buffer + ram_load_buffer_size - !first,
2406 2407 2408 2409 2410 2411 2412
                   rtb,
                   m->data.ram_transmit.nbytes);

            /* Increment the buffer size counter, remembering to add 1 if this */
            /* is the first time we've allocated data, to allow room for a     */
            /* string terminator when the transfer finishes.                   */

2413
            ram_load_buffer_size += m->data.ram_transmit.nbytes + !!first;
2414 2415 2416 2417 2418

            /* If tracing the process of the transfer, show the buffer contents */
            /* (but don't bother showing the uninitialised last byte).          */

            #ifdef TRACE
2419 2420
              if (tl & (1u<<28)) trace_dump_buffer(ram_load_uri_buffer,
                                                   ram_load_buffer_size - 1,
2421
                                                   2);
2422
            #endif
2423 2424
          }
        }
2425

2426
        /* If the transfer has finished, fetch the URL */
2427

2428 2429 2430 2431 2432
        if (finished)
        {
          /* Remember we allocated 1 byte more than needed at the start, */
          /* so that this terminator could be inserted. This is required */
          /* for URL files, and won't hurt for URI files.                */
2433

2434
          ram_load_uri_buffer[ram_load_buffer_size - 1] = 0;
2435

2436
          /* This is capable of dealing with URI or URL files */
2437

2438
          urlutils_extract_uri(ram_load_uri_buffer, ram_load_buffer_size);
2439

2440 2441 2442
          if (pending_data_save_ack->data.data_save_ack.destination_window <= 0)
          {
            /* The original Message_DataSave was for no specific window */
2443

2444
            e = windows_create_browser(ram_load_uri_buffer, NULL, NULL, NULL, Windows_CreateBrowser_Normal);
2445
          }
2446

2447
          #ifndef REMOTE_HOTLIST
2448

2449 2450 2451 2452
            else
            {
              /* As with DataLoad, this can go to a Hotlist window or a browser; */
              /* the hotlist is the simpler case, check that first.              */
2453

2454 2455
              ObjectId this;
              int      handled = 0;
2456

2457
              /* If we can get the window object ID... */
2458

2459 2460 2461 2462 2463 2464 2465 2466 2467
              if (
                   !window_wimp_to_toolbox(0,
                                           pending_data_save_ack->data.data_save_ack.destination_window,
                                           -1,
                                           &this,
                                           NULL)
                 )
              {
                /* ...and it matches that of the hotlist, add the item. */
2468

2469 2470
                if (this == hotlist_return_window_id())
                {
2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
                  /* Use the title, if we have one */

                  if (*(ram_load_uri_buffer + strlen(ram_load_uri_buffer) + 1))
                  {
                    ChkError(hotlist_add_position(pending_data_save_ack->data.data_save_ack.destination_x,
                                                  pending_data_save_ack->data.data_save_ack.destination_y,
                                                  ram_load_uri_buffer + strlen(ram_load_uri_buffer) + 1,
                                                  ram_load_uri_buffer));
                  }
                  else
                  {
                    ChkError(hotlist_add_position(pending_data_save_ack->data.data_save_ack.destination_x,
                                                  pending_data_save_ack->data.data_save_ack.destination_y,
                                                  ram_load_uri_buffer,
                                                  ram_load_uri_buffer));
                  }
2487

2488 2489 2490
                  handled = 1;
                }
              }
2491

2492 2493 2494
              if (!handled)
              {
                browser_data * b;
2495

2496
                /* The message was for a specific window, so find out which one */
2497

2498
                utils_browser_from_window(pending_data_save_ack->data.data_save_ack.destination_window, &b);
2499

2500 2501 2502
                if (b && !b->small_fetch) e = fetchpage_new(b, ram_load_uri_buffer, 1, 0);
              }
            }
2503

2504
          #endif
2505

2506 2507
          free (ram_load_uri_buffer);
          ram_load_uri_buffer = NULL;
2508

2509 2510 2511 2512
          #ifdef TRACE
            malloccount -= ram_load_buffer_size;
            if (tl & (1u<<13)) Printf("** malloccount (protocols_atatl_got_ram_transmit): \0212%d\0217\n",malloccount);
          #endif
2513

2514
          ram_load_buffer_size = 0;
2515

2516
          if (!e) ok = 0;
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
        }
      }
      break;

      default:
      {
        /* If we don't recognise the filetype, something is very wrong... */

        #ifdef TRACE

          erb.errnum = Utils_Error_Custom_Normal;
          sprintf(erb.errmess,
2529
                  "Unrecognised filetype 0x%03x (%x) in protocols_atatl_got_ram_transmit",
2530 2531
                  pending_data_save_ack->data.data_save_ack.file_type,
                  pending_data_save_ack->data.data_save_ack.file_type);
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545

          e  = &erb;
          ok = 0;

        #else

          ok = 0;

        #endif
      }
      break;
    }
  }

2546
  if (ok && !finished)
2547 2548 2549 2550 2551 2552 2553
  {
    /* If things are OK still, send a RAMFetch to get the  */
    /* next chunk, provided that there is more to get.     */
    /*                                                     */
    /* If things are not OK, no reply is given, thereby    */
    /* abandoning the transfer (PRM 3-256).                */

2554
    WimpMessage * raf = m;
2555

2556 2557 2558
    raf->hdr.size        = 28;
    raf->hdr.your_ref    = m->hdr.my_ref;
    raf->hdr.action_code = Wimp_MRAMFetch;
2559

2560 2561 2562
    #ifdef TRACE
      if (tl & (1u<<28)) Printf("protocols_atatl_got_ram_transmit: Sending RAMFetch in reply\n");
    #endif
2563

2564
    e = wimp_send_message(Wimp_EUserMessageRecorded,
2565 2566 2567 2568
                          raf,
                          m->hdr.sender,
                          0,
                          NULL);
2569 2570

    ram_fetch_reference = m->hdr.my_ref;
2571 2572
  }

2573
  /* Either we've finished or there was an error higher up */
2574 2575 2576

  if (finished || !ok)
  {
2577 2578
    /* Free the RAM transfer buffer, if required. */

2579 2580 2581
    if (rtb) free(rtb);
    rtb = NULL;

2582 2583 2584 2585 2586 2587 2588
    #ifdef TRACE
      malloccount -= RTB_Size;
      if (tl & (1u<<13)) Printf("** malloccount (protocols_atatl_got_ram_transmit): \0212%d\0217\n",malloccount);
    #endif

    /* Free any pending Message_DataSaveAck */

2589 2590
    if (pending_data_save_ack)
    {
2591 2592 2593 2594
      #ifdef TRACE
        if (tl & (1u<<28)) Printf("protocols_atatl_got_ram_transmit: Freeing old pending Acknowledge block\n");
      #endif

2595
      free(pending_data_save_ack);
2596
      pending_data_save_ack = NULL;
2597
    }
2598 2599 2600 2601 2602
  }

  return e;
}

2603
/*************************************************/
2604
/* protocols_pp_send_print_save()                */
2605
/*                                               */
2606 2607
/* First stage of the Print Protocol - broadcast */
/* a Message_PrintSave.                          */
2608
/*                                               */
2609
/* See PRM 3-259, 262.                           */
2610 2611
/*************************************************/

2612
_kernel_oserror * protocols_pp_send_print_save(void)
2613
{
2614
  WimpMessage m;
2615

2616
  /* Fill in the header */
2617

2618 2619
  m.hdr.your_ref    = 0;
  m.hdr.action_code = Browser_Message_PrintSave;
2620

2621
  /* Fill in the body */
2622

2623 2624 2625 2626
  m.data.data_save.destination_window = 0;
  m.data.data_save.destination_icon   = 0;
  m.data.data_save.destination_x      = 0;
  m.data.data_save.destination_y      = 0;
2627
  m.data.data_save.estimated_size     = 4096;
2628
  m.data.data_save.file_type          = FileType_POUT;
2629

2630 2631
  StrNCpy0(m.data.data_save.leaf_name,
           lookup_token("PrintName:WebPage",0,0));
2632

2633
  /* Work out the message size */
2634

2635 2636
  m.hdr.size = (strlen(m.data.data_save.leaf_name) + 44);
  if (m.hdr.size & 3) m.hdr.size = (m.hdr.size & ~3) + 4;
2637

2638
  /* Send the message */
2639

2640 2641 2642 2643 2644
  RetError(wimp_send_message(Wimp_EUserMessageRecorded,
                             &m,
                             0,
                             0,
                             NULL));
2645

2646 2647 2648
  /* A Message_DataSaveAck returns, so to distinguish this from */
  /* any other Message_DataSaveAck, remember my_ref and check   */
  /* it against the returned your_ref in future.                */
2649

2650
  print_save_reference = m.hdr.my_ref;
2651 2652 2653 2654

  return NULL;
}

2655
/*************************************************/
2656
/* protocols_pp_print_save_bounced()             */
2657
/*                                               */
2658 2659 2660 2661 2662
/* If a Message_PrintSave bounces, the browser   */
/* tried to use the printer protocol to do a     */
/* print job but the printer wasn't loaded. In   */
/* that case, print directly to the 'printer:'   */
/* device.                                       */
2663
/*                                               */
2664 2665 2666
/* See PRM 3-259 for details of where this fits  */
/* into the protocol, or 3-262 for the message   */
/* structure.                                    */
2667 2668 2669 2670 2671
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message.         */
/*************************************************/

2672
_kernel_oserror * protocols_pp_print_save_bounced(WimpMessage * m)
2673
{
2674
  if (m->hdr.my_ref && m->hdr.my_ref == print_save_reference)
2675
  {
2676
    print_save_reference = 0;
2677

2678
    /* Go for it. */
2679

2680 2681
    print_print(NULL);
  }
2682

2683
  #ifdef TRACE
2684

2685
    else
2686 2687
    {
      erb.errnum = Utils_Error_Custom_Normal;
2688

2689 2690 2691
      sprintf(erb.errmess,
              "Message reference %x not recognised in protocols_pp_print_save_bounced",
              m->hdr.your_ref);
2692

2693 2694
      return &erb;
    }
2695

2696
  #endif
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706

  return NULL;
}

/*************************************************/
/* protocols_pp_got_print_error()                */
/*                                               */
/* Handle reception of a Message_PrintError -    */
/* report an error from the printer driver.      */
/*                                               */
2707
/* See PRM 3-262.                                */
2708 2709
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
2710
/*             for the received message.         */
2711 2712
/*************************************************/

2713
_kernel_oserror * protocols_pp_got_print_error(WimpMessage * m)
2714
{
2715
  if (m->hdr.size == 20)
2716
  {
2717
    /* RISC OS 2 printer manager's PrintBusy response */
2718

2719
    erb.errnum = Utils_Error_Custom_Message;
2720

2721 2722 2723 2724
    StrNCpy0(erb.errmess,
             lookup_token("PrintBusy:The printer is currently busy.",
                          0,
                          0));
2725

2726
    show_error_ret(&erb);
2727 2728
  }

2729 2730 2731
  /* RISC OS 3 !Printers-generated specific error response */

  else show_error_ret((_kernel_oserror *) &m->data);
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753

  return NULL;
}

/*************************************************/
/* protocols_pp_got_data_save_ack()              */
/*                                               */
/* Handle reception of a Message_DataSaveAck     */
/* where the your_ref field shows that it has    */
/* been sent in relation to a print job. Only    */
/* call in those circumstances...                */
/*                                               */
/* See PRM 3-259 / 3-260 for details of where    */
/* this fits into the protocol, or PRM 3-253     */
/* for details on the message structure.         */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message. The     */
/*             contents will be modified and     */
/*             used in a reply.                  */
/*************************************************/

2754
static _kernel_oserror * protocols_pp_got_data_save_ack(WimpMessage * m)
2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
{
  _kernel_oserror * e;
  WimpMessage     * dl = m;
  int               file_size;

  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_pp_got_data_save_ack: Called\n");
  #endif

  /* Print to a file in the !Printers-given file, then send */
  /* a DataLoad to the printer manager to tell it to put    */
  /* the file into the print queue.                         */

2768
  print_save_reference = 0;
2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797

  print_print(m->data.data_save.leaf_name);

  e = _swix(OS_File,
        _INR(0,1) | _OUT(4),

        23, /* Read catalogue info */
        m->data.data_save.leaf_name,

        &file_size);

  if (e) return e;

  dl->hdr.size        = 64;
  dl->hdr.your_ref    = m->hdr.my_ref;
  dl->hdr.action_code = Wimp_MDataLoad;

  dl->data.data_load.estimated_size = file_size;
  dl->data.data_load.file_type      = FileType_POUT;

  e = _swix(OS_File,
            _INR(0,2),

            18, /* Set filetype of named object */
            m->data.data_save_ack.leaf_name,
            FileType_POUT);

  strcpy(dl->data.data_load.leaf_name, m->data.data_save_ack.leaf_name);

2798 2799
  /* Send the DataLoad - a UserMessage, not UserMessageRecorded, */
  /* so it can't bounce back.                                    */
2800 2801 2802 2803 2804

  #ifdef TRACE
    if (tl & (1u<<28)) Printf("protocols_pp_got_data_save_ack: Sending Message_DataLoad in response\n");
  #endif

2805 2806 2807 2808 2809
  return wimp_send_message(Wimp_EUserMessage,
                           dl,
                           m->hdr.sender,
                           0,
                           NULL);
2810 2811 2812
}

/*************************************************/
2813
/* protocols_pp_got_print_type_odd()             */
2814
/*                                               */
2815
/* Handle reception of a Message_PrintTypeOdd.   */
2816
/*                                               */
2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
/* If this is sent directly to the task, we can  */
/* print immediately to 'printer:'.              */
/*                                               */
/* The use of an Alias$PrintType_FF4 system      */
/* variable makes it unnecessary to deal with    */
/* the case of a broadcast PrintTypeOdd. This    */
/* happens when a queued PrintOut file that the  */
/* browser has been asked to print rises to the  */
/* top of !Printers' queue. Bizarrely, !Printers */
/* doesn't recognise the filetype and raises the */
/* message. We allow this to fall back to the    */
/* system variable, which copies the PrintOut    */
/* file to the 'printer:' device.                */
/*                                               */
/* This is a hole in an otherwise workable, if   */
/* cumbersome, protocol.                         */
/*                                               */
/* See PRM 3-263.                                */
2835 2836
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
2837 2838 2839
/*             for the received message. The     */
/*             contents will be modified and     */
/*             used in a reply.                  */
2840 2841
/*************************************************/

2842
_kernel_oserror * protocols_pp_got_print_type_odd(WimpMessage * m)
2843
{
2844
  WimpMessage * ptk = m;
2845

2846
  if (m->hdr.your_ref && m->hdr.your_ref == print_save_reference)
2847
  {
2848 2849
    /* The printer manager sent PrintTypeOdd as a reply to this */
    /* task (not a broadcast), so go ahead and print.           */
2850

2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863
    print_save_reference = 0;

    /* Send PrintTypeKnown */

    ptk->hdr.size        = 20;
    ptk->hdr.your_ref    = m->hdr.my_ref;
    ptk->hdr.action_code = Browser_Message_PrintTypeKnown;

    RetError(wimp_send_message(Wimp_EUserMessage,
                               ptk,
                               m->hdr.sender,
                               0,
                               NULL));
2864 2865 2866 2867

    print_print(NULL);
  }

2868 2869
  #ifdef TRACE

2870
    else if (m->hdr.your_ref)
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914
    {
      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Message reference %x unrecognised in protocols_pp_got_print_type_odd",
              m->hdr.your_ref);

      return &erb;
    }

  #endif

// Commented out as the Alias$@PrintType_FF4 system variable does this job
// anyway, and if we don't claim this message then anything else which may
// have a better idea of what to do at least gets a chance to try.
//
// This currently doesn't work, incidentally; the conditions on the 'if'
// are wrong (print_save_ref has probably been set to 0, but I never
// got the chance to properly debug this before removing it due to time
// constraints...).
//
//   else if (print_save_ref && m->data.data_save.file_type == FileType_POUT)
//   {
//     /* If the printer doesn't understand PrintOut files, then */
//     /* it may be broken (!) / PostScript. So reply, and copy  */
//     /* the file to the printer device directly.               */
//
//     print_save_ref = 0;
//
//     ptk->hdr.size        = 20;
//     ptk->hdr.your_ref    = m->hdr.my_ref;
//     ptk->hdr.action_code = Browser_Message_PrintTypeKnown;
//
//     ChkError(wimp_send_message(Wimp_EUserMessage, ptk, m->hdr.sender, 0, NULL));
//
//     _swix(OS_FSControl,
//           _INR(0,3),
//
//           26,
//           m->data.data_save.leaf_name,
//           "printer:",
//           2); /* Flags - 'Force' set, but no others. */
//   }

2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
  return NULL;
}

/*************************************************/
/* protocols_auh_got_process()                   */
/*                                               */
/* Handle reception of a URI_MProcess - process  */
/* a URI from the URI handler, replying if we    */
/* can handle the URI.                           */
/*                                               */
/* See 1307,260/FS.                              */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message. The     */
/*             contents will be modified and     */
/*             used in a reply.                  */
/*************************************************/

_kernel_oserror * protocols_auh_got_process(WimpMessage * m)
{
  URIProcessMessage * uri    = (URIProcessMessage *) &m->data;
  int                 ok;
  unsigned int        sender = m->hdr.sender;

  /* Can we handle this URI? */

  ok = urlutils_check_protocols(uri->uri);

  #ifdef TRACE
    if (tl & (1u<<21)) Printf("protocols_auh_got_process: URI_MProcess '%s', ok = %d\n",uri->uri,ok);
  #endif

  /* If so, reply to the message and possibly start a fetch */

  if (ok)
  {
2951 2952 2953 2954 2955 2956 2957
    #ifndef SINGLE_USER

      if (logged_in)
      {

    #endif

2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974
    /* Only fetch if the flags bits don't say we're to just */
    /* check the URI could be handled.                      */

    if (!uri->flags.bits.check)
    {
      uri_queue * entry = urlutils_find_queue_entry(uri->uri_handle);

      if (entry)
      {
        RetError(fetchpage_postprocess_uri(entry->b,
                                           uri  ->uri,
                                           entry->flags & URIQueue_RecordInHistory ? 1 : 0));

        /* Don't remove it from the queue of uri_queue structures yet - */
        /* wait for the ReturnResult message for that.                  */
      }

2975
      else RetError(windows_create_browser(uri->uri, NULL, NULL, NULL, Windows_CreateBrowser_Normal));
2976 2977
    }

2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
    #ifndef SINGLE_USER

      }
      else
      {
        erb.errnum = Utils_Error_Custom_Message;

        StrNCpy0(erb.errmess,
                 lookup_token("MustLogIn:The browser cannot fetch Web pages until you log in.",
                              0,
                              0));

        show_error_ret(&erb);
      }

    #endif

2995 2996 2997 2998 2999 3000
    /* Now reply, saying that we've handled the message */

    m->hdr.sender      = task_handle;
    m->hdr.your_ref    = m->hdr.my_ref;
    m->hdr.action_code = URI_MProcessAck;

3001 3002 3003 3004 3005
    RetError(wimp_send_message(Wimp_EUserMessage,
                               m,
                               sender,
                               0,
                               NULL));
3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092
  }

  return NULL;
}

/*************************************************/
/* protocols_auh_got_return_result()             */
/*                                               */
/* Handle reception of a URI_MReturnResult -     */
/* the URI handler is reporting what happened to */
/* a URI we dispatched through it.               */
/*                                               */
/* See 1307,260/FS.                              */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message. The     */
/*             contents will be modified and     */
/*             used in a reply.                  */
/*************************************************/

_kernel_oserror * protocols_auh_got_return_result(WimpMessage * m)
{
  URIReturnResultMessage * uri = (URIReturnResultMessage *) &m->data;

  #ifdef TRACE
    if (tl & (1u<<21)) Printf("protocols_auh_got_return_result: URI_MReturnResult, not_claimed = %d\n",uri->flags.bits.not_claimed);
  #endif

  /* Remove the entry from the queue */

  RetError(urlutils_remove_from_queue(uri->uri_handle));

  /* If the URI was not claimed by anyone, give an appropriate error */

  if (uri->flags.bits.not_claimed)
  {
    erb.errnum = Utils_Error_Custom_Message;

    StrNCpy0(erb.errmess,
             lookup_token("CannotFetch:The browser does not have a method of fetching the requested site.",
                          0,0));

    show_error_ret(&erb);
  }

  return NULL;
}

/*************************************************/
/* protocols_auh_got_dying()                     */
/*                                               */
/* Handle reception of a URI_MDying - the URI    */
/* handler is closing down.                      */
/*                                               */
/* See 1307,260/FS.                              */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message.         */
/*************************************************/

_kernel_oserror * protocols_auh_got_dying(WimpMessage * m)
{
  /* If the URI handler is dying, don't try and use it anymore... */

  uri_module_present = 0;

  return NULL;
}

/*************************************************/
/* protocols_ih_send_help_request()              */
/*                                               */
/* Sends out HelpRequest messages for the item   */
/* the mouse pointer is currently over, every    */
/* 20 centiseconds or so.                        */
/*                                               */
/* Parameters are as standard for a Wimp NULL    */
/* event handler.                                */
/*************************************************/

int protocols_ih_send_help_request(int eventcode, WimpPollBlock * block, IdBlock * idb, void * handle)
{
  int        time_now;
  static int last_time   = 0;
  static int last_window = 0;
  static int last_icon   = 0;

3093
  /* Only proceed if the Controls say to do so */
3094

3095
  if (controls.claim_help)
3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137
  {
    /* Don't sent out requests too often */

    _swix(OS_ReadMonotonicTime, _OUT(0), &time_now);

    if (time_now - last_time > 20)
    {
      WimpGetPointerInfoBlock i;
      WimpMessage             m;

      last_time = time_now;

      ChkError(wimp_get_pointer_info(&i));

      /* Don't send a request if the pointer isn't over a */
      /* browser-owned window.                            */

      if (task_handle == task_from_window(i.window_handle))
      {
        /* Don't send out multiple requests for the same window/icon. */

        if (i.icon_handle != last_icon || i.window_handle != last_window)
        {
          last_icon   = i.icon_handle;
          last_window = i.window_handle;
        }
        else return 0;

        /* Build the message block and send the request */

        m.hdr.size        = 40;
        m.hdr.sender      = task_handle;
        m.hdr.my_ref      = 0;
        m.hdr.your_ref    = 0;
        m.hdr.action_code = Wimp_MHelpRequest;

        m.data.help_request.mouse_x       = i.x;
        m.data.help_request.mouse_y       = i.y;
        m.data.help_request.buttons       = i.button_state;
        m.data.help_request.window_handle = i.window_handle;
        m.data.help_request.icon_handle   = i.icon_handle;

3138 3139 3140 3141 3142
        ChkError(wimp_send_message(Wimp_EUserMessageRecorded,
                                   &m,
                                   i.window_handle,
                                   i.icon_handle,
                                   NULL));
3143 3144 3145 3146 3147 3148 3149
      }
    }
  }

  return 0;
}

3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170
/*************************************************/
/* protocols_ih_help_request_bounced()           */
/*                                               */
/* If a Message_HelpRequest comes in as a        */
/* UserMessage_Acknowledge, then the browser     */
/* tried to send out such a message and it       */
/* bounced - there is no help on that item.      */
/* In this case, if we're displaing help items   */
/* in the status bar, return to a non-help       */
/* status string.                                */
/*                                               */
/* See PRM 3-244 for details of the message or   */
/* protocols_ih_send_help_request for the        */
/* message origin.                               */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message.         */
/*************************************************/

_kernel_oserror * protocols_ih_help_request_bounced(WimpMessage * m)
{
3171
  if (controls.claim_help)
3172 3173
  {
    WimpGetPointerInfoBlock   i;
3174 3175
    browser_data            * b = NULL;
    ObjectId                  o, a = -1;
3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206

    RetError(wimp_get_pointer_info(&i));
    RetError(window_wimp_to_toolbox(0, i.window_handle, i.icon_handle, &o, NULL));

    /* If we can get an ancestor, the pointer is over e.g. a toolbar */
    /* - otherwise, assume it is over a browser window.              */

    toolbox_get_ancestor(0, o, &a, NULL);

    if (a)
    {
      toolbox_get_client_handle(0, a, (void *) &b);
    }
    else toolbox_get_client_handle(0, o, (void *) &b);

    /* If we haven't got a valid client handle, exit */

    if (!is_known_browser(b)) return NULL;

    /* Update the status line */

    if (b->status_help != NULL)
    {
      b->status_help = NULL;
      RetError(toolbars_cancel_status(b, Toolbars_Status_Help));
    }
  }

  return NULL;
}

3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
/*************************************************/
/* protocols_ih_got_help_reply()                 */
/*                                               */
/* On receiving a Message_HelpReply, we may want */
/* to display the Help text in the status bar.   */
/*                                               */
/* See PRM 3-245.                                */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message.         */
/*************************************************/

_kernel_oserror * protocols_ih_got_help_reply(WimpMessage * m)
{
3221
  static char last_help[Limits_StatusBarStatus];
3222 3223 3224

  /* Don't do anything if the Controls say not to. */

3225
  if (controls.claim_help)
3226 3227
  {
    WimpGetPointerInfoBlock   i;
3228 3229
    browser_data            * b = NULL;
    ObjectId                  o, a = 0;
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248

    RetError(wimp_get_pointer_info(&i));
    RetError(window_wimp_to_toolbox(0, i.window_handle, i.icon_handle, &o, NULL));

    /* If we can get an ancestor, the pointer is over e.g. a toolbar */
    /* - otherwise, assume it is over a browser window.              */

    toolbox_get_ancestor(0, o, &a, NULL);

    if (a)
    {
      toolbox_get_client_handle(0, a, (void *) &b);
    }
    else toolbox_get_client_handle(0, o, (void *) &b);

    /* If we haven't got a valid client handle, exit. */

    if (!is_known_browser(b)) return NULL;

3249 3250 3251 3252
    /* If this is a small fetch window, don't display anything either */

    if (b->small_fetch) return NULL;

3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287
    /* If the text is empty, there was no help for that item, */
    /* so restore the old status display, if there was a      */
    /* help display already there.                            */

    if (!*m->data.help_reply.text)
    {
      if (b->status_help != NULL)
      {
        b->status_help = NULL;
        RetError(toolbars_cancel_status(b, Toolbars_Status_Help));
      }
    }
    else
    {
      /* Otherwise update the status bar with the help text, */
      /* if the text has changed.                            */

      if (
           !b->status_help ||
           (
             b->status_help                                  &&
             strcmp(b->status_help, m->data.help_reply.text)
           )
         )
      {
        StrNCpy0(last_help, m->data.help_reply.text);
        b->status_help = last_help;
        RetError(toolbars_update_status(b, Toolbars_Status_Help));
      }
    }
  }

  return NULL;
}

3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377
/*************************************************/
/* protocols_aub_got_open_url()                  */
/*                                               */
/* Handle ANT Open URL broadcasts.               */
/*                                               */
/* See http://www.ant.co.uk/support/tech/        */
/* notes/url.html (15 Sep 1997).                 */
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message. The     */
/*             contents will be modified and     */
/*             used in a reply.                  */
/*************************************************/

_kernel_oserror * protocols_aub_got_open_url(WimpMessage * m)
{
  ant_open_url * open = (ant_open_url *) &m->data;
  int            ok   = 0;
  char         * url;
  char         * target;

  /* If 'tag' is non-NULL, the data is a URL string */

  if (open->tag) url = (char *) open, target = NULL;

  /* Otherwise, it's indirected */

  else
  {
    char * file;
    char * mimetype;

    /* Entries are either NULL, an offset into the  */
    /* message block (but we have to ensure that it */
    /* is a valid offset), or a pointer. This       */
    /* protocol sucks, big time.                    */

    if (!open->file.offset) file = NULL;
    else if (open->file.offset < 0x1800000)
    {
      if (open->file.offset >= sizeof(m->data.bytes)) file = NULL;
      else file = ((char *) open) + open->file.offset;
    }
    else file = open->file.ptr;

    if (!open->mimetype.offset) mimetype = NULL;
    else if (open->mimetype.offset < 0x1800000)
    {
      if (open->mimetype.offset >= sizeof(m->data.bytes)) mimetype = NULL;
      else mimetype = ((char *) open) + open->mimetype.offset;
    }
    else mimetype = open->mimetype.ptr;

    /* Can't handle file or mimetype requests */

    if (!file && !mimetype)
    {
      if (!open->url.offset) url = NULL;
      else if (open->url.offset < 0x1800000)
      {
        if (open->url.offset >= sizeof(m->data.bytes)) url = NULL;
        else url = ((char *) open) + open->url.offset;
      }
      else url = open->url.ptr;

      if (!open->target.offset) target = NULL;
      else if (open->target.offset < 0x1800000)
      {
        if (open->target.offset >= sizeof(m->data.bytes)) target = NULL;
        else target = ((char *) open) + open->target.offset;
      }
      else target = open->target.ptr;
    }
    else url = NULL, target = NULL;
  }

  /* Check the protocol - can we handle this? */

  if (url) ok = urlutils_check_protocols(url);

  /* If so, do so */

  if (ok)
  {
    /* Send out the URL */

    RetError(windows_create_browser(url,
                                    NULL,
                                    NULL,
                                    target,
3378
                                    Windows_CreateBrowser_Normal));
3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393

    /* Acknowledge the message */

    m->hdr.your_ref = m->hdr.my_ref;

    return wimp_send_message(Wimp_EUserMessageAcknowledge,
                             m,
                             m->hdr.sender,
                             0,
                             NULL);
  }

  return NULL;
}

3394
/*************************************************/
3395
/* protocols_multi_got_data_save_ack()           */
3396
/*                                               */
3397 3398 3399
/* Got a DataSaveAck - this could be part of the */
/* print protocol, or standard file transfer     */
/* between applications.                         */
3400 3401 3402 3403 3404
/*                                               */
/* Parameters: Pointer to the WimpMessage struct */
/*             for the received message.         */
/*************************************************/

3405
_kernel_oserror * protocols_multi_got_data_save_ack(WimpMessage * m)
3406
{
3407 3408
  if (m->hdr.your_ref && m->hdr.your_ref == print_save_reference) return protocols_pp_got_data_save_ack(m);
  else                                                            return protocols_atats_got_data_save_ack(m);
3409
}
3410

3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
/*************************************************/
/* protocols_util_update_reference()             */
/*                                               */
/* When a message is sent out as part of an      */
/* ongoing conversation, this function should be */
/* called after Wimp_SendMessage to update the   */
/* internal records of known my_ref numbers.     */
/*                                               */
/* Parameters: The value of the your_ref field   */
/*             in the *original* message, before */
/*             a reply was generated (so you     */
/*             will need to remember this before */
/*             setting your_ref to my_ref prior  */
/*             to replying);                     */
/*                                               */
/*             The value of the my_ref field     */
/*             filled in by the Wimp after       */
/*             Wimp_SendMessage was called to    */
/*             send the reply.                   */
/*************************************************/
3431

3432
static void protocols_util_update_reference(int old_ref, int new_ref)
3433 3434
{
  /* This would look really great as a switch statement. But hey, */
3435 3436 3437
  /* I'm sure C is really an excellent language. Really.          */

  if      (old_ref == save_source_reference)              save_source_reference            = new_ref;
3438
  else if (old_ref == save_as_text_reference)             save_as_text_reference           = new_ref;
3439
  else if (old_ref == save_as_draw_reference)             save_as_draw_reference           = new_ref;
3440 3441 3442 3443 3444 3445 3446 3447 3448 3449
  else if (old_ref == save_link_reference)                save_link_reference              = new_ref;
  else if (old_ref == save_location_reference)            save_location_reference          = new_ref;

  #ifndef REMOTE_HOTLIST

    else if (old_ref == save_hotlist_entry_reference)     save_hotlist_entry_reference     = new_ref;
    else if (old_ref == save_hotlist_selection_reference) save_hotlist_selection_reference = new_ref;
    else if (old_ref == save_entire_hotlist_reference)    save_entire_hotlist_reference    = new_ref;

  #endif
3450

3451
  else if (old_ref == save_object_reference)              save_object_reference            = new_ref;
3452
  else if (old_ref == save_image_reference)               save_image_reference             = new_ref;
3453
  else if (old_ref == save_history_reference)             save_history_reference           = new_ref;
3454
  else if (old_ref == print_save_reference)               print_save_reference             = new_ref;
3455

3456
  #ifdef TRACE
3457

3458
    else
3459
    {
3460
      erb.errnum = Utils_Error_Custom_Normal;
3461

3462
      sprintf(erb.errmess,
3463
              "Old message reference %x not recognised in protocols_util_update_reference (has a new save method been created, but not added to this function?)",
3464
              old_ref);
3465

3466
      show_error_ret(&erb);
3467 3468
    }

3469
  #endif
3470
}
3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492

/*************************************************/
/* protocols_util_make_unique_name()             */
/*                                               */
/* Generates a browser-unique pathname in the    */
/* given buffer, based on Wimp$ScrapDir. This is */
/* hopefully system-unique as the function       */
/* keeps going until it can't find a file under  */
/* the name it has constructed.                  */
/*                                               */
/* Parameters: Pointer to the buffer;            */
/*                                               */
/*             Size of the buffer.               */
/*                                               */
/* Assumes:    Well it won't crash if you give   */
/*             it a NULL buffer pointer or a     */
/*             buffer size of less than 2 bytes, */
/*             but there's not exactly a huge    */
/*             amount of worth in calling the    */
/*             function under those conditions!  */
/*************************************************/

3493
void protocols_util_make_unique_name(char * buffer, int buffer_size)
3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561
{
  int    filecount = 0;
  int    type;
  char * leaf;

  if (!buffer || buffer_size < 2) return;

  /* Clear the buffer first */

  memset(buffer, 0, buffer_size);

  /* Write '<Wimp$ScrapDir>.' (or whatever is defined in Save.h) */
  /* to the buffer - +9 is 8 letters for the leafname (see code  */
  /* below) plus 1 for a terminator.                             */

  if (strlen(Save_ScrapPath) + 9 <= buffer_size) strcpy(buffer, Save_ScrapPath);
  else return;

  /* Append with a unique 8 letter leafname */

  leaf = buffer + strlen(buffer);

  do
  {
    sprintf(leaf, "BTF%05x", filecount++);

    /* Can we find the file? */

    type = 1;

    _swix(OS_File,
          _INR(0,1) | _OUT(0),

          17, /* Read catalogue info for object */
          buffer,

          &type);

    /* Keep going until we run out of files (!) or find an unused name */
  }
  while (type != 0 && filecount <= 0xfffff);

  /* Woo - did we run out? */

  if (filecount > 0xfffff)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      StrNCpy0(erb.errmess,
               "Blimey! Ran out of filenames in protocols_util_make_unique_name. Going to try to use Save_ScrapFile instead...");

      show_error_ret(&erb);

    #endif

    *buffer = 0;

    if (strlen(Save_ScrapFile) + 1 <= buffer_size) strcpy(buffer, Save_ScrapFile);

    return;
  }

  /* Finished */

  return;
}