2022-04-10 08:22:58 +00:00
|
|
|
// Copyright 2022 Dietrich Epp.
|
|
|
|
// This file is part of SyncFiles. SyncFiles is licensed under the terms of the
|
|
|
|
// Mozilla Public License, version 2.0. See LICENSE.txt for details.
|
2022-11-16 23:19:05 +00:00
|
|
|
#include "macos/project.h"
|
2022-04-10 08:09:17 +00:00
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
#include "lib/crc32.h"
|
|
|
|
#include "macos/choose_directory.h"
|
|
|
|
#include "macos/error.h"
|
|
|
|
#include "macos/main.h"
|
|
|
|
#include "macos/path.h"
|
|
|
|
#include "macos/resources.h"
|
|
|
|
#include "macos/strutil.h"
|
|
|
|
#include "macos/tempfile.h"
|
2022-04-10 08:09:17 +00:00
|
|
|
|
2022-11-16 23:32:52 +00:00
|
|
|
#include <Aliases.h>
|
2022-04-10 08:09:17 +00:00
|
|
|
#include <MacMemory.h>
|
|
|
|
#include <MacWindows.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
Project format:
|
|
|
|
|
|
|
|
Header:
|
|
|
|
byte[32] magic
|
|
|
|
uint32 version
|
2022-11-16 23:19:05 +00:00
|
|
|
uint32 header length
|
|
|
|
uint32 header crc32
|
2022-04-10 08:09:17 +00:00
|
|
|
uint32 chunk count
|
|
|
|
ckinfo[] chunk info
|
|
|
|
byte[] chunk data
|
|
|
|
|
|
|
|
Chunk info:
|
2022-11-16 23:19:05 +00:00
|
|
|
uint32 chunk ID
|
2022-04-10 08:09:17 +00:00
|
|
|
uint32 byte offset
|
|
|
|
uint32 byte length
|
2022-11-16 23:19:05 +00:00
|
|
|
uint32 chunk crc32
|
2022-04-10 08:09:17 +00:00
|
|
|
|
|
|
|
Chunks: (names zero-padded)
|
2022-11-16 23:19:05 +00:00
|
|
|
"LOCA" local directory alias
|
2023-05-06 18:03:13 +00:00
|
|
|
"LOCP" local directory path
|
2022-11-16 23:19:05 +00:00
|
|
|
"REMA" destination directory alias
|
2023-05-06 18:03:13 +00:00
|
|
|
"REMP" destination directory path
|
2022-04-10 08:09:17 +00:00
|
|
|
|
|
|
|
Chunks are aligned to 16 byte boundaries, and the file is padded to a 16 byte
|
|
|
|
boundary. This is just to make it easier to read hexdumps.
|
|
|
|
*/
|
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
#define kVersion 0x10000
|
|
|
|
|
2022-04-10 08:09:17 +00:00
|
|
|
// clang-format off
|
|
|
|
|
|
|
|
// Magic cookie that identifies project files.
|
2022-11-16 23:19:05 +00:00
|
|
|
static const unsigned char kMagic[32] = {
|
2022-04-10 08:09:17 +00:00
|
|
|
// Identify the file type.
|
2022-11-16 23:19:05 +00:00
|
|
|
'S', 'y', 'n', 'c', 'F', 'i', 'l', 'e', 's',
|
|
|
|
' ', 'P', 'r', 'o', 'j', 'e', 'c', 't',
|
2022-04-10 08:09:17 +00:00
|
|
|
// Detect newline conversions.
|
|
|
|
0, 10, 0, 13, 0, 13, 10, 0,
|
|
|
|
// Detect encoding conversions (infinity in Roman and UTF-8).
|
2022-11-16 23:19:05 +00:00
|
|
|
0xb0, 0, 0xe2, 0x88, 0x9e, 0, 0
|
2022-04-10 08:09:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
struct ProjectHeader {
|
|
|
|
UInt8 magic[32];
|
|
|
|
UInt32 version;
|
|
|
|
UInt32 size;
|
|
|
|
UInt32 crc32;
|
|
|
|
UInt32 chunkCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ProjectChunk {
|
2022-11-16 23:19:05 +00:00
|
|
|
UInt32 id;
|
2022-04-10 08:09:17 +00:00
|
|
|
UInt32 offset;
|
|
|
|
UInt32 size;
|
2022-11-16 23:19:05 +00:00
|
|
|
UInt32 crc32;
|
2022-04-10 08:09:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Dimensions of controls.
|
|
|
|
enum {
|
|
|
|
kVBorder = 10,
|
|
|
|
kHBorder = 10,
|
|
|
|
kDirVSize = 62,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kControlChooseDirLocal,
|
|
|
|
kControlChooseDirRemote,
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: this is redundant.
|
|
|
|
enum {
|
|
|
|
kDirLocal,
|
|
|
|
kDirRemote,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ProjectDir {
|
|
|
|
int pathLength;
|
|
|
|
Handle path;
|
|
|
|
AliasHandle alias;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A synchronization project.
|
|
|
|
struct Project {
|
|
|
|
int windowWidth;
|
|
|
|
int isActive;
|
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
// File reference to the project file, or fileRef == 0 otherwise.
|
2022-04-10 08:09:17 +00:00
|
|
|
short fileRef;
|
2022-11-16 23:19:05 +00:00
|
|
|
// Location of the project file, if vRefNum != 0. In a possible edge case,
|
|
|
|
// the fileSpec may be set even when fileRef is 0. This may happen if you
|
|
|
|
// save the project to a volume which does not support FSpExchangeFiles(),
|
|
|
|
// and the operation fails.
|
2022-04-10 08:09:17 +00:00
|
|
|
FSSpec fileSpec;
|
2022-11-16 23:19:05 +00:00
|
|
|
// Script code for the filename.
|
|
|
|
ScriptCode fileScript;
|
2022-04-10 08:09:17 +00:00
|
|
|
|
|
|
|
struct ProjectDir dirs[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
void ProjectNew(void)
|
|
|
|
{
|
|
|
|
ProjectHandle project;
|
|
|
|
WindowRef window;
|
|
|
|
ControlRef control;
|
|
|
|
struct Project *projectp;
|
|
|
|
int i, windowWidth, controlWidth;
|
|
|
|
|
|
|
|
project = (ProjectHandle)NewHandle(sizeof(struct Project));
|
|
|
|
if (project == NULL) {
|
|
|
|
EXIT_ASSERT(NULL);
|
|
|
|
}
|
|
|
|
window = GetNewCWindow(rWIND_Project, NULL, (WindowPtr)-1);
|
|
|
|
if (window == NULL) {
|
|
|
|
EXIT_ASSERT(NULL);
|
|
|
|
}
|
|
|
|
windowWidth = window->portRect.right - window->portRect.left;
|
|
|
|
SetWRefCon(window, (long)project);
|
|
|
|
projectp = *project;
|
|
|
|
memset(projectp, 0, sizeof(*projectp));
|
|
|
|
projectp->windowWidth = windowWidth;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
control = GetNewControl(rCNTL_ChooseFolder, window);
|
|
|
|
if (control == NULL) {
|
|
|
|
EXIT_ASSERT(NULL);
|
|
|
|
}
|
|
|
|
controlWidth =
|
|
|
|
(*control)->contrlRect.right - (*control)->contrlRect.left;
|
|
|
|
MoveControl(control, windowWidth - controlWidth - kHBorder,
|
|
|
|
kVBorder + kDirVSize * i + 22);
|
|
|
|
SetControlReference(control, kControlChooseDirLocal + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
ShowWindow(window);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProjectAdjustMenus(ProjectHandle project, MenuHandle fileMenu)
|
|
|
|
{
|
|
|
|
(void)project;
|
|
|
|
if (fileMenu != NULL) {
|
|
|
|
EnableItem(fileMenu, iFile_Close);
|
|
|
|
EnableItem(fileMenu, iFile_Save);
|
|
|
|
EnableItem(fileMenu, iFile_SaveAs);
|
|
|
|
EnableItem(fileMenu, iFile_Revert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProjectClose(WindowRef window, ProjectHandle project)
|
|
|
|
{
|
|
|
|
struct Project *projectp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
KillControls(window);
|
|
|
|
DisposeWindow(window);
|
|
|
|
HLock((Handle)project);
|
|
|
|
projectp = *project;
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (projectp->dirs[i].path != NULL) {
|
|
|
|
DisposeHandle(projectp->dirs[i].path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DisposeHandle((Handle)project);
|
|
|
|
}
|
|
|
|
|
2023-05-06 18:03:13 +00:00
|
|
|
#define kChunkCount 4
|
2022-04-10 08:09:17 +00:00
|
|
|
|
2022-11-16 23:30:59 +00:00
|
|
|
#define ALIGN(x) (((x) + 15) & ~(UInt32)15)
|
2022-04-10 08:09:17 +00:00
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
static OSErr ProjectWriteHeader(short refNum, int nchunks,
|
|
|
|
struct ProjectChunk *ckInfo)
|
2022-04-10 08:09:17 +00:00
|
|
|
{
|
2022-11-16 23:19:05 +00:00
|
|
|
Ptr ptr;
|
|
|
|
struct ProjectHeader *head;
|
|
|
|
UInt32 size;
|
|
|
|
long count;
|
|
|
|
OSErr err;
|
2022-04-10 08:09:17 +00:00
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
size = sizeof(struct ProjectHeader) + sizeof(struct ProjectChunk) * nchunks;
|
|
|
|
ptr = NewPtr(size);
|
|
|
|
if (ptr == NULL) {
|
2022-04-10 08:09:17 +00:00
|
|
|
return MemError();
|
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
head = (struct ProjectHeader *)ptr;
|
|
|
|
memcpy(head->magic, kMagic, sizeof(head->magic));
|
|
|
|
head->version = kVersion;
|
|
|
|
head->size = size;
|
|
|
|
head->crc32 = 0;
|
|
|
|
head->chunkCount = nchunks;
|
|
|
|
memcpy(ptr + sizeof(struct ProjectHeader), ckInfo,
|
|
|
|
nchunks * sizeof(struct ProjectChunk));
|
|
|
|
head->crc32 = CRC32Update(0, ptr, size);
|
|
|
|
|
|
|
|
count = size;
|
|
|
|
err = FSWrite(refNum, &count, ptr);
|
|
|
|
DisposePtr(ptr);
|
|
|
|
return err;
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
static const UInt32 kProjectAliasChunks[2] = {'LOCA', 'REMA'};
|
2023-05-06 18:03:13 +00:00
|
|
|
static const UInt32 kProjectPathChunks[2] = {'LOCP', 'REMP'};
|
2022-11-16 23:19:05 +00:00
|
|
|
|
|
|
|
static OSErr ProjectFlush(short refNum)
|
2022-04-10 08:09:17 +00:00
|
|
|
{
|
|
|
|
ParamBlockRec pb;
|
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
memset(&pb, 0, sizeof(pb));
|
|
|
|
pb.ioParam.ioRefNum = refNum;
|
|
|
|
return PBFlushFileSync(&pb);
|
|
|
|
}
|
2022-04-10 08:09:17 +00:00
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
// ProjectWriteContent writes the project data to the given file.
|
|
|
|
static OSErr ProjectWriteContent(ProjectHandle project, short refNum)
|
|
|
|
{
|
|
|
|
UInt32 pad[4];
|
|
|
|
OSErr err;
|
|
|
|
Handle ckData[kChunkCount];
|
|
|
|
struct ProjectChunk ckInfo[kChunkCount];
|
|
|
|
UInt32 off;
|
|
|
|
Size size;
|
|
|
|
Handle h;
|
|
|
|
int i, nchunks;
|
|
|
|
long count;
|
2022-04-10 08:09:17 +00:00
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
// Gather all chunks.
|
|
|
|
nchunks = 0;
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
h = (Handle)(*project)->dirs[i].alias;
|
|
|
|
if (h != NULL) {
|
|
|
|
ckData[nchunks] = h;
|
|
|
|
ckInfo[nchunks].id = kProjectAliasChunks[i];
|
|
|
|
nchunks++;
|
|
|
|
}
|
2023-05-06 18:03:13 +00:00
|
|
|
h = (*project)->dirs[i].path;
|
|
|
|
if (h != NULL) {
|
|
|
|
ckData[nchunks] = h;
|
|
|
|
ckInfo[nchunks].id = kProjectPathChunks[i];
|
|
|
|
nchunks++;
|
|
|
|
}
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
// Calculate chunk offsets.
|
|
|
|
off = sizeof(struct ProjectHeader) + sizeof(struct ProjectChunk) * nchunks;
|
|
|
|
for (i = 0; i < nchunks; i++) {
|
|
|
|
size = GetHandleSize(ckData[i]);
|
|
|
|
ckInfo[i].offset = off;
|
|
|
|
ckInfo[i].size = size;
|
|
|
|
ckInfo[i].crc32 = CRC32Update(0, *ckData[i], size);
|
|
|
|
off = ALIGN(off + size);
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
// Write file. Refer to IM: Files listing 1-9, page 1-24.
|
|
|
|
err = SetEOF(refNum, off);
|
2022-04-10 08:09:17 +00:00
|
|
|
if (err != noErr) {
|
2022-11-16 23:19:05 +00:00
|
|
|
return err;
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
err = ProjectWriteHeader(refNum, nchunks, ckInfo);
|
2022-04-10 08:09:17 +00:00
|
|
|
if (err != noErr) {
|
2022-11-16 23:19:05 +00:00
|
|
|
return err;
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
pad[i] = 0;
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
for (i = 0; i < nchunks; i++) {
|
|
|
|
size = ckInfo[i].size;
|
|
|
|
count = size;
|
|
|
|
err = FSWrite(refNum, &count, *ckData[i]);
|
|
|
|
if (err != noErr) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
count = (-size) & 15;
|
|
|
|
if (count > 0) {
|
|
|
|
err = FSWrite(refNum, &count, pad);
|
|
|
|
if (err != noErr) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A SaveMode describes the exact type of save operation.
|
|
|
|
typedef enum {
|
|
|
|
// kSaveNew indicates that the file is new, it does not exist yet.
|
|
|
|
kSaveNew,
|
|
|
|
// kSaveReplace indicates that the file is being saved over an existing
|
|
|
|
// file.
|
|
|
|
kSaveReplace,
|
|
|
|
// kSaveUpdate indicates that the file is being updated in-place.
|
|
|
|
kSaveUpdate
|
|
|
|
} SaveMode;
|
|
|
|
|
|
|
|
// ProjectCloseFile closes any open files and sets the file ref to 0.
|
|
|
|
static void ProjectCloseFile(ProjectHandle project)
|
|
|
|
{
|
|
|
|
struct Project *projectp;
|
|
|
|
|
2022-04-10 08:09:17 +00:00
|
|
|
projectp = *project;
|
|
|
|
if (projectp->fileRef != 0) {
|
|
|
|
FSClose(projectp->fileRef);
|
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
}
|
2022-04-10 08:09:17 +00:00
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
// ProjectSetFile closes any open files and replaces them with the given file.
|
|
|
|
static void ProjectSetFile(ProjectHandle project, short fileRef, FSSpec *spec)
|
|
|
|
{
|
|
|
|
struct Project *projectp;
|
|
|
|
|
|
|
|
projectp = *project;
|
|
|
|
if (projectp->fileRef != 0) {
|
|
|
|
FSClose(projectp->fileRef);
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
projectp->fileRef = fileRef;
|
|
|
|
memcpy(&projectp->fileSpec, spec, sizeof(FSSpec));
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
// ProjectWrite writes the project to a file.
|
|
|
|
static OSErr ProjectWrite(ProjectHandle project, FSSpec *spec,
|
|
|
|
ScriptCode script, SaveMode mode)
|
2022-04-10 08:09:17 +00:00
|
|
|
{
|
2022-11-16 23:19:05 +00:00
|
|
|
HParamBlockRec hb;
|
|
|
|
CMovePBRec cm;
|
2022-04-10 08:09:17 +00:00
|
|
|
FSSpec temp;
|
|
|
|
OSErr err;
|
2022-11-16 23:19:05 +00:00
|
|
|
short refNum;
|
2022-04-10 08:09:17 +00:00
|
|
|
|
2023-04-20 06:07:14 +00:00
|
|
|
// See IM: Files, p. 1-25, Listing 1-10 "Updating a File Safely".
|
|
|
|
|
2022-11-16 23:19:05 +00:00
|
|
|
err = MakeTempFile(spec->vRefNum, &temp);
|
2022-04-10 08:09:17 +00:00
|
|
|
if (err != noErr) {
|
2022-11-16 23:19:05 +00:00
|
|
|
return err;
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
err = FSpCreate(&temp, kCreator, kTypeProject, script);
|
2022-04-10 08:09:17 +00:00
|
|
|
if (err != noErr) {
|
2022-11-16 23:19:05 +00:00
|
|
|
return err;
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
err = FSpOpenDF(&temp, fsRdWrPerm, &refNum);
|
2022-04-10 08:09:17 +00:00
|
|
|
if (err != noErr) {
|
2022-11-16 23:19:05 +00:00
|
|
|
goto error1;
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
err = ProjectWriteContent(project, refNum);
|
2022-04-10 08:09:17 +00:00
|
|
|
if (err != noErr) {
|
2022-11-16 23:19:05 +00:00
|
|
|
goto error2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For ordinary "Save", swap the file contents.
|
|
|
|
if (mode == kSaveUpdate) {
|
|
|
|
err = FSpExchangeFiles(&temp, spec);
|
|
|
|
if (err == noErr) {
|
|
|
|
err = FlushVol(NULL, spec->vRefNum);
|
|
|
|
if (err != noErr) {
|
|
|
|
FSClose(refNum);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
ProjectSetFile(project, refNum, spec);
|
|
|
|
// FIXME: What error handling, here?
|
|
|
|
FSpDelete(&temp);
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
// paramErr: function not supported by volume.
|
|
|
|
if (err != paramErr) {
|
|
|
|
goto error2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ProjectCloseFile(project);
|
|
|
|
if (mode != kSaveNew) {
|
|
|
|
// For replace or update, the file already exists.
|
|
|
|
err = FSpDelete(spec);
|
|
|
|
if (err != noErr && err != fnfErr) {
|
|
|
|
goto error2;
|
|
|
|
}
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
|
|
|
|
// Try move & rename.
|
|
|
|
memset(&hb, 0, sizeof(hb));
|
|
|
|
hb.copyParam.ioNamePtr = temp.name;
|
|
|
|
hb.copyParam.ioVRefNum = temp.vRefNum;
|
|
|
|
hb.copyParam.ioNewName = spec->name;
|
|
|
|
hb.copyParam.ioNewDirID = spec->parID;
|
|
|
|
hb.copyParam.ioDirID = temp.parID;
|
|
|
|
err = PBHMoveRenameSync(&hb);
|
|
|
|
if (err == noErr) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
// paramErr: function not supported by volume.
|
|
|
|
if (err != paramErr) {
|
|
|
|
goto error2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try move, then rename.
|
|
|
|
memset(&cm, 0, sizeof(cm));
|
|
|
|
cm.ioNamePtr = temp.name;
|
|
|
|
cm.ioVRefNum = temp.vRefNum;
|
|
|
|
cm.ioNewDirID = spec->parID;
|
|
|
|
cm.ioDirID = temp.parID;
|
|
|
|
err = PBCatMoveSync(&cm);
|
2022-04-10 08:09:17 +00:00
|
|
|
if (err != noErr) {
|
2022-11-16 23:19:05 +00:00
|
|
|
goto error2;
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
temp.parID = spec->parID;
|
|
|
|
err = FSpRename(&temp, spec->name);
|
2022-04-10 08:09:17 +00:00
|
|
|
if (err != noErr) {
|
2022-11-16 23:19:05 +00:00
|
|
|
goto error2;
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
done:
|
|
|
|
err = FlushVol(NULL, spec->vRefNum);
|
2022-04-10 08:09:17 +00:00
|
|
|
if (err != noErr) {
|
2022-11-16 23:19:05 +00:00
|
|
|
FSClose(refNum);
|
|
|
|
return err;
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:19:05 +00:00
|
|
|
ProjectSetFile(project, refNum, spec);
|
|
|
|
return noErr;
|
|
|
|
|
|
|
|
error2:
|
|
|
|
FSClose(refNum);
|
|
|
|
error1:
|
2022-04-10 08:09:17 +00:00
|
|
|
FSpDelete(&temp);
|
2022-11-16 23:19:05 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A SaveCommand distinguishes between Save and Save As.
|
|
|
|
typedef enum { kSave, kSaveAs } SaveCommand;
|
|
|
|
|
|
|
|
// ProjectSave handles the Save and SaveAs menu commands.
|
|
|
|
static void ProjectSave(WindowRef window, ProjectHandle project,
|
|
|
|
SaveCommand cmd)
|
|
|
|
{
|
|
|
|
// See listing 1-12 in IM: Files.
|
|
|
|
Str255 name;
|
|
|
|
StandardFileReply reply;
|
|
|
|
OSErr err;
|
|
|
|
struct ErrorParams errp;
|
|
|
|
FSSpec spec;
|
|
|
|
ScriptCode script;
|
|
|
|
|
|
|
|
if (cmd == kSaveAs || (*project)->fileSpec.vRefNum == 0) {
|
|
|
|
GetWTitle(window, name);
|
2023-04-20 06:07:14 +00:00
|
|
|
// FIXME: Move string to resource.
|
2022-11-16 23:19:05 +00:00
|
|
|
StandardPutFile("\pSave project as:", name, &reply);
|
|
|
|
if (!reply.sfGood) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ProjectWrite(project, &reply.sfFile, reply.sfScript,
|
|
|
|
reply.sfReplacing ? kSaveReplace : kSaveNew);
|
|
|
|
if (err != noErr) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
SetWTitle(window, reply.sfFile.name);
|
|
|
|
} else {
|
|
|
|
memcpy(&spec, &(*project)->fileSpec, sizeof(FSSpec));
|
|
|
|
script = (*project)->fileScript;
|
|
|
|
err = ProjectWrite(project, &spec, script, kSaveUpdate);
|
|
|
|
if (err != noErr) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-10 08:09:17 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
memset(&errp, 0, sizeof(errp));
|
|
|
|
errp.err = kErrCouldNotSaveProject;
|
|
|
|
errp.osErr = err;
|
2022-11-16 23:19:05 +00:00
|
|
|
errp.strParam = reply.sfFile.name;
|
2022-04-10 08:09:17 +00:00
|
|
|
ShowError(&errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProjectCommand(WindowRef window, ProjectHandle project, int menuID,
|
|
|
|
int item)
|
|
|
|
{
|
|
|
|
switch (menuID) {
|
|
|
|
case rMENU_File:
|
|
|
|
switch (item) {
|
|
|
|
case iFile_Close:
|
|
|
|
ProjectClose(window, project);
|
|
|
|
break;
|
|
|
|
case iFile_Save:
|
2022-11-16 23:19:05 +00:00
|
|
|
ProjectSave(window, project, kSave);
|
2022-04-10 08:09:17 +00:00
|
|
|
break;
|
|
|
|
case iFile_SaveAs:
|
2022-11-16 23:19:05 +00:00
|
|
|
ProjectSave(window, project, kSaveAs);
|
2022-04-10 08:09:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProjectUpdate(WindowRef window, ProjectHandle project)
|
|
|
|
{
|
|
|
|
struct Project *projectp;
|
|
|
|
Rect rect;
|
|
|
|
Handle text;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
HLock((Handle)project);
|
|
|
|
|
|
|
|
projectp = *project;
|
|
|
|
rect = window->portRect;
|
|
|
|
EraseRect(&rect);
|
|
|
|
MoveTo(0, kVBorder + kDirVSize - 10);
|
|
|
|
LineTo(projectp->windowWidth, kVBorder + kDirVSize - 10);
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
MoveTo(kHBorder, kVBorder + kDirVSize * i + 12);
|
|
|
|
DrawString(i == 0 ? "\pLocal Folder" : "\pRemote Folder");
|
|
|
|
text = projectp->dirs[i].path;
|
|
|
|
if (text != NULL) {
|
|
|
|
HLock(text);
|
|
|
|
MoveTo(kHBorder, kVBorder + kDirVSize * i + 36);
|
|
|
|
DrawText(*text, 0, projectp->dirs[i].pathLength);
|
|
|
|
HUnlock(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UpdateControls(window, window->visRgn);
|
|
|
|
|
|
|
|
HUnlock((Handle)project);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProjectActivate(WindowRef window, ProjectHandle project, int isActive)
|
|
|
|
{
|
|
|
|
struct Project *projectp;
|
|
|
|
|
|
|
|
projectp = *project;
|
|
|
|
if (projectp->isActive != isActive) {
|
|
|
|
projectp->isActive = isActive;
|
|
|
|
InvalRect(&window->portRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 18:03:13 +00:00
|
|
|
// VolumeDoesSupportIDs returns true if the volume supports file IDs. If the
|
|
|
|
// volume does not support file IDs, then we fall back to using paths to refer
|
|
|
|
// to project directories. This applies to the Basilisk II "Unix" volume. If you
|
|
|
|
// try to resolve an alias to a file on the Basilisk II "Unix" volume, you may
|
|
|
|
// get a different file than intended, because the directory IDs are not stable.
|
|
|
|
static Boolean VolumeDoesSupportIDs(short vRefNum)
|
|
|
|
{
|
|
|
|
HParamBlockRec hb;
|
|
|
|
GetVolParmsInfoBuffer parms;
|
|
|
|
OSErr err;
|
|
|
|
|
|
|
|
memset(&hb, 0, sizeof(hb));
|
|
|
|
hb.ioParam.ioVRefNum = vRefNum;
|
|
|
|
hb.ioParam.ioBuffer = (Ptr)&parms;
|
|
|
|
hb.ioParam.ioReqCount = sizeof(parms);
|
|
|
|
err = PBHGetVolParms(&hb, false);
|
|
|
|
if (err != noErr) {
|
|
|
|
ExitErrorOS(kErrUnknown, err);
|
|
|
|
}
|
|
|
|
return (parms.vMAttrib & (1ul << bHasFileIDs)) != 0;
|
|
|
|
}
|
|
|
|
|
2022-04-10 08:09:17 +00:00
|
|
|
static void ProjectChooseDir(WindowRef window, ProjectHandle project,
|
|
|
|
int whichDir)
|
|
|
|
{
|
|
|
|
struct Project *projectp;
|
|
|
|
struct ProjectDir *dirp;
|
|
|
|
FSSpec directory;
|
2022-11-16 23:32:52 +00:00
|
|
|
AliasHandle alias;
|
2022-04-10 08:09:17 +00:00
|
|
|
Handle path;
|
|
|
|
int pathLength;
|
|
|
|
OSErr err;
|
|
|
|
|
2022-11-16 23:32:52 +00:00
|
|
|
if (!ChooseDirectory(&directory)) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-06 18:03:13 +00:00
|
|
|
if (VolumeDoesSupportIDs(directory.vRefNum)) {
|
|
|
|
err = NewAlias(NULL, &directory, &alias);
|
|
|
|
if (err != noErr) {
|
|
|
|
ExitErrorOS(kErrUnknown, err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
alias = NULL;
|
2022-11-16 23:32:52 +00:00
|
|
|
}
|
|
|
|
err = GetDirPath(&directory, &pathLength, &path);
|
|
|
|
if (err != noErr) {
|
|
|
|
ExitErrorOS(kErrUnknown, err);
|
|
|
|
}
|
|
|
|
projectp = *project;
|
|
|
|
dirp = &projectp->dirs[whichDir];
|
|
|
|
if (dirp->path != NULL) {
|
|
|
|
DisposeHandle(dirp->path);
|
|
|
|
}
|
|
|
|
dirp->pathLength = pathLength;
|
|
|
|
dirp->path = path;
|
|
|
|
if (dirp->alias != NULL) {
|
|
|
|
DisposeHandle((Handle)dirp->alias);
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
2022-11-16 23:32:52 +00:00
|
|
|
dirp->alias = alias;
|
|
|
|
InvalRect(&window->portRect);
|
2022-04-10 08:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProjectMouseDown(WindowRef window, ProjectHandle project,
|
|
|
|
EventRecord *event)
|
|
|
|
{
|
|
|
|
Point mouse;
|
|
|
|
GrafPtr savedPort;
|
|
|
|
ControlRef control;
|
|
|
|
ControlPartCode part;
|
|
|
|
long reference;
|
|
|
|
|
|
|
|
GetPort(&savedPort);
|
|
|
|
SetPort(window);
|
|
|
|
mouse = event->where;
|
|
|
|
GlobalToLocal(&mouse);
|
|
|
|
part = FindControl(mouse, window, &control);
|
|
|
|
switch (part) {
|
|
|
|
case kControlButtonPart:
|
|
|
|
reference = GetControlReference(control);
|
|
|
|
if (TrackControl(control, mouse, NULL) != 0) {
|
|
|
|
switch (reference) {
|
|
|
|
case kControlChooseDirLocal:
|
|
|
|
ProjectChooseDir(window, project, kDirLocal);
|
|
|
|
break;
|
|
|
|
case kControlChooseDirRemote:
|
|
|
|
ProjectChooseDir(window, project, kDirRemote);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SetPort(savedPort);
|
|
|
|
}
|