home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
linuxmafia.com 2016
/
linuxmafia.com.tar
/
linuxmafia.com
/
pub
/
palmos
/
progect-src-0.20.tar.gz
/
progect-src-0.20.tar
/
progect-0.20
/
link.c
< prev
next >
Wrap
C/C++ Source or Header
|
2000-10-26
|
15KB
|
657 lines
/* -*-Mode:C; tab-width:4; indent-tabs-mode:t; c-file-style:"stroustrup";-*- */
// $Id: link.c,v 1.13 2000/10/10 17:08:48 seagull_kamome Exp $
#include "MemoDB.h"
#include "AddressDB.h"
#include "MemoMain.h"
#include "link.h"
#include "task.h"
#include "flat.h"
#include "progect.h"
#include "progectdb.h"
#include "progectRsc.h"
/****************************************************************************
* Name : AddLinkRecord
* Desc : add a link record to the database
* In :
* -> database
* -> uniqueID of the link
* -> index of task to add a link to
* -> type of link
* -> name of the DB to link to
* Auth : lb, 22.09.2000
* Rem : doesn't handle err codes
***************************************************************************/
Err AddLinkRecord(DmOpenRef dbP, UInt32 uniqueID, UInt16 *index,
AppLinkType app, Char *dbName)
{
MemHandle h;
TaskExtendedRecordType *p;
TaskExtendedRecordType task;
UInt32 size = sizeof(TaskExtendedRecordType);
Err Err = errNone;
UInt16 realIndex;
Boolean withDbName = false;
// get a handle on a newly created record
// new one always comes after actual
if (DmNumRecords(dbP) == 0)
{
realIndex = 0;
}
else if (DmNumRecords(dbP) > 1 && TaskGetHasChild(dbP, gParentTask))
{
realIndex = TaskGetNextRelativeIndex(dbP, *index); // place of new one
}
else
{
realIndex = gParentTask + 1;
}
if (!realIndex)
{
realIndex = dmMaxRecordIndex;
}
// set the fields for a link record
task.attr.allBits = 0;
task.attr.bits.opened = 1;
task.format.allBits = 0;
task.format.bits.extendedType = 1;
task.fields.link.uniqueID = uniqueID;
task.fields.link.dbName = '\0';
switch (app)
{
case memoLink:
task.format.bits.isMemo = 1;
break;
case addressLink:
task.format.bits.isContact = 1;
break;
case progectLink:
task.format.bits.isProject = 1;
withDbName = true;
size += StrLen(dbName);
break;
default:
return pgError;
break;
}
h = DmNewRecord(dbP, &realIndex, size);
if (!h)
{
DEBUG1("Not enough memory for a new task");
return pgError;
}
// retrieve info about user of previous index
// this sets : level, next
if (*index == 0 || !TaskGetHasChild(dbP, gParentTask))
{
// this is a first child
task.attr.bits.level = gRefLevel + 1;
TaskSetHasChild(dbP, gParentTask, true);
}
else
{
task.attr.bits.level = TaskGetLevel(dbP, *index);
task.attr.bits.hasPrev = 1;
task.attr.bits.hasNext = TaskGetHasNext(dbP, *index);
// update hasNext field in previous
TaskSetHasNext(dbP, *index, true);
}
// get a pointer on the record
p = MemHandleLock(h);
// write the data
DmWrite(p, 0, &task, sizeof(TaskExtendedRecordType));
if (withDbName)
DmStrCopy(p, OffsetOf(TaskExtendedRecordType, fields.link.dbName), dbName);
// unlock the pointer
MemHandleUnlock(h);
// unlock the record
DmReleaseRecord(dbP, realIndex, true);
*index = realIndex;
return Err;
} // Err AddLinkRecord(DmOpenRef dbP, UInt32 uniqueID, UInt16 *index, AppLinkType app)
/****************************************************************************
* Name : LinkCreateLinkTree
* Desc : create a tree from the memo database
* Parm :
* Out : pgErr
* Auth : lb, 12.09.2000
***************************************************************************/
pgErr LinkCreateLinkTree(DmOpenRef dbP, AppLinkType app)
{
CurrentPrefsType saveCurrent = gCurrentPrefs;
Boolean restoreDBstate = false;
UInt16 cardNo = 0;
DmOpenRef linkDB, sourceDB;
UInt16 numRec, insPos, i;
UInt32 uniqueID;
LocalID dbID;
Char *linkDBname, *prepLinkDBname;
switch (app)
{
case memoLink:
// open the memo db from memopad
MemoGetDatabase(&sourceDB, dmModeReadWrite);
linkDBname = "MemoDB";
prepLinkDBname = "lbPG-MemoDB";
break;
case addressLink:
// open the address db from address
AddrGetDatabase(&sourceDB, dmModeReadWrite);
linkDBname = "AddressDB";
prepLinkDBname = "lbPG-AddressDB";
break;
default:
return pgError;
break;
}
// if a db is opened now, we must open it back when done
if (dbP)
{
restoreDBstate = true;
CloseDB(dbP);
}
gParentTask = gRefLevel = 0;
// if memodb exists, delete it
if ((dbID = DmFindDatabase(cardNo, prepLinkDBname)))
{
// remove it
DmDeleteDatabase(cardNo, dbID);
}
// create a db for the memos
CreateDB(linkDBname);
// open the link db (db with links to the memos)
dbID = DmFindDatabase(cardNo, prepLinkDBname);
linkDB = DmOpenDatabase(cardNo, dbID, dmModeReadWrite);
gParentTask = gRefLevel = 0;
numRec = DmNumRecords(sourceDB);
// add a link to each memo the the new link db
insPos = 0;
for (i = 0; i < numRec; i++)
{
UInt16 attr;
// get the memo's UniqueID
DmRecordInfo(sourceDB, i, &attr, &uniqueID, NULL);
// create the link if it's not deleted
if (! (attr & dmRecAttrDelete))
AddLinkRecord(linkDB, uniqueID, &insPos, app, NULL);
}
// close both databases
DmCloseDatabase(sourceDB);
DmCloseDatabase(linkDB);
// if we had a database, reopen it
if (restoreDBstate)
{
OpenDB(saveCurrent.openDBName, &gdbP);
}
return pgOK;
} // pgErr LinkCreateLinkTree(void)
/***************************************************************************
** LinkMaster support functions
**************************************************************************/
// TODO:
// 1.Multiple link
//
/****************************************************************************
* Name : CheckLinkMaster
* Desc : Check to LinkMaster is available
* Parm :
* Out : true if available, otherwise false.
* Auth : seagull, 22.09.2000 JST
***************************************************************************/
static Boolean CheckLinkMaster(void)
{
if (! LinkCheck())
{
MessageBox(StrLinkMasterIsNotAvailable);
return false;
}
return true;
} // static Boolean CheckLinkMaster(void)
/****************************************************************************
* Name : PublishToLinkMaster
* Desc : Publish bookbark to LinkMaster
* Parm :
* -> database pointer
* -> index of the task
* Out :
* Auth : seagull, 22.09.2000 JST
***************************************************************************/
void PublishToLinkMaster(DmOpenRef dbP, UInt16 index)
{
if (CheckLinkMaster())
{
Char* desc = TaskGetDescription(dbP, index);
LinkSimpleType* p = LinkSimpleNew(dbP, index, desc);
MemPtrFree(desc);
// NOTE: LinkSimpleNew need index. that gots unique id from index.
LinkPublish((LinkInfoPtr)p);
}
} // void PublishToLinkMaster(DmOpenPref dpP, UInt16 index)
/****************************************************************************
* Name : RequestLinkViaLinkMaster
* Desc : Request link info
* Parm :
* -> database pointer
* -> index of the task
* Out :
* Auth : seagull, 22.09.2000 JST
***************************************************************************/
void RequestLinkViaLinkMaster(Char const* dbName, UInt16 index)
{
if (CheckLinkMaster())
{
LinkSimplePtr info = MemPtrNew(sizeof(LinkSimpleType));
info->creator = CREATOR;
DmRecordInfo(gdbP, index, NULL, &info->record_id, NULL);
StrCopy(info->db_name, dbName);
LinkRequest((LinkInfoPtr)info);
}
} // void RequestLinkViaLinkMaster(Char const* dbName, UInt16 index)
/****************************************************************************
* Name : SetLinkMasterLink
* Desc : Store link info into record.
* Parm :
* -> LinkInfo of this task
* -> LinkInfo of target info.
* Out :
* Auth : seagull, 22.09.2000 JST
* Mod : lb, 26.09.2000
* - use of task functions to modify format of task
***************************************************************************/
void SetLinkMasterLink(LinkSimplePtr p1, LinkInfoPtr p2)
{
UInt16 index;
TaskFormatType format;
DmFindRecordByID(gdbP, p1->record_id, &index);
// NOTE: currently the subkey is must be zero.
TaskSetExtraChunk(gdbP, index, Extra_Link_LinkMaster, 0,
p2, sizeof(LinkInfoType));
// Turn ON hasLink bit.
// same using task functions
format = TaskGetFormat(gdbP, index);
format.hasLink = true;
TaskSetFormat(gdbP, index, format);
} // void SetLinkMasterLink(LinkSimplePtr p1, LinkInfoPtr p2)
/****************************************************************************
* Name : FollowLinkViaLinkMaster
* Desc : fllow link master link
* Parm :
* -> database name
* -> uniq index
* Out :
* Auth : seagull, 22.09.2000 JST
***************************************************************************/
void FollowLinkViaLinkMaster(UInt16 index)
{
LinkInfoType info;
UInt16 size = sizeof(info);
if (TaskGetExtraChunk(gdbP, index, Extra_Link_LinkMaster, 0x00,
&info, &size) == pgOK &&
size == sizeof(info))
{
LinkFollow(&info);
}
} // void FollowLinkViaLinkMaster(Char dbname, UInt16 index)
/****************************************************************************
* Name : RemoveLinkViaLinkMaster
* Desc : remove link info
* Parm :
* -> uniq index
* Out :
* Auth : seagull, 22.09.2000 JST
***************************************************************************/
void RemoveLinkViaLinkMaster(UInt16 index)
{
TaskFormatType format = TaskGetFormat(gdbP, index);
if (format.hasLink)
{
TaskRemoveExtraChunk(gdbP, index, Extra_Link_LinkMaster, 0);
// Turn OFF hasLink bit
format.hasLink = false;
TaskSetFormat(gdbP, index, format);
}
} // void RemoveLinkViaLinkMaster(UInt16 index)
/****************************************************************************
* Name : ExportToMemo
* Desc : export a project to MemoPad files
* In : database
* Auth : lb, 21.09.2000
***************************************************************************/
pgErr ExportToMemo(DmOpenRef dbP, MemoExportOptionsType options)
{
#define predesc 100
Char *buffer; // to store the text to export
MemoItemType memo;
UInt16 cnt = 0, i, numRec, offset = 0, index = dmMaxRecordIndex, tab, level;
Err err;
DmOpenRef memoDB;
UInt16 memoNb = 1;
buffer = MemPtrNew(memoMaxLength);
numRec = DmNumRecords(dbP);
StrCopy(buffer, &gCurrentPrefs.openDBName[5]);
offset = StrLen(&gCurrentPrefs.openDBName[5]);
buffer[offset++] = (Char)chrLineFeed;
cnt = offset;
// for each record
for (i = 1; i < numRec; i++)
{
Char* desc;
// skip it if it's done and we don't want done tasks
if (!options.exportDone &&
(TaskGetCompleted(dbP, i) == 10 ||
TaskGetCompleted(dbP, i) == ACTION_OK))
{
continue;
}
// skip it if it's not a terminal and we just want terminals
if (options.exportFlat && !FlatFilter(dbP, i))
{
continue;
}
level = TaskGetLevel(dbP, i) - 1;
desc = TaskGetDescription(dbP, i);
cnt += StrLen(desc) + level;
// control the length
if (cnt >= memoMaxLength - predesc)
{
// begin new memo
buffer[offset] = '\0';
// memorize it
err = MemoGetDatabase (&memoDB, dmModeReadWrite);
if (err)
{
DEBUG1("Can't open memo database");
return pgError;
}
memo.note = buffer;
err = MemoNewRecord (memoDB, &memo, &index);
if (err)
{
DEBUG1("Can't create a new memo");
return pgError;
}
DmCloseDatabase(memoDB);
// reinit buffer
StrCopy(buffer, &gCurrentPrefs.openDBName[5]);
offset = StrLen(&gCurrentPrefs.openDBName[5]);
buffer[offset++] = ' ';
buffer[offset++] = '(';
StrIToA(gDebugVal, ++memoNb);
StrCopy(&buffer[offset], gDebugVal);
offset += StrLen(gDebugVal);
buffer[offset++] = ')';
buffer[offset++] = (Char)chrLineFeed;
cnt = offset;
}
// indent if not flat view
if (!options.exportFlat)
{
for (tab = 1; tab <= level; tab++)
{
buffer[offset++] = chrHorizontalTabulation;
}
}
// export progress
if (options.exportProgress)
{
UInt8 progress = TaskGetCompleted(dbP, i);
if (progress > ACTION)
{
if (progress == ACTION_OK)
{
StrCopy(&buffer[offset], "[x] ");
}
else
{
StrCopy(&buffer[offset], "[ ] ");
}
offset += 4;
}
else
{
buffer[offset++] = '[';
StrIToA(gDebugVal, progress * 10);
StrCopy(&buffer[offset], gDebugVal);
offset += StrLen(gDebugVal);
buffer[offset++] = '%';
buffer[offset++] = ']';
buffer[offset++] = ' ';
}
}
// export priority
if (options.exportPriority)
{
UInt8 priority = TaskGetPriority(dbP, i);
if (priority > 0 && priority < 6)
{
buffer[offset++] = '[';
buffer[offset++] = priority + '0';
buffer[offset++] = ']';
buffer[offset++] = ' ';
}
}
// export due date
if (options.exportDueDate)
{
DateType date = TaskGetDueDate(dbP, i);
if (date.month != 0)
{
DateToAscii(date.month, date.day,
date.year + YEAR_OFFSET, gDateFormat, gDebugVal);
buffer[offset++] = '(';
StrCopy(&buffer[offset], gDebugVal);
offset += StrLen(&buffer[offset]);
buffer[offset++] = ')';
buffer[offset++] = ' ';
}
}
// copy the description
StrCopy(&buffer[offset], desc);
MemPtrFree(desc);
offset += StrLen(&buffer[offset]);
// new line
buffer[offset] = (Char)chrLineFeed;
offset++;
cnt = offset;
}
buffer[offset] = '\0';
// memorize it
err = MemoGetDatabase (&memoDB, dmModeReadWrite);
if (err)
{
DEBUG1("Can't open memo database");
return pgError;
}
memo.note = buffer;
MemoNewRecord (memoDB, &memo, &index);
DmCloseDatabase(memoDB);
MemPtrFree(buffer);
return pgOK;
} // pgErr ExportToMemo(DmOpenRef dbP, MemoExportOptionsType options)
/****************************************************************************
* Name : DirectLinkFollow
* Desc : follow a link
* In : the linkfield info
* Auth : lb, 28.09.2000
***************************************************************************/
pgErr DirectLinkFollow(DmOpenRef dbP, UInt16 index)
{
GoToParamsType *params;
TaskExtendedRecordType *p;
MemHandle h;
LocalID appID;
DmSearchStateType searchstate;
DmOpenRef linkDB;
UInt16 cardNo, attr;
UInt32 appNum = 0;
Err err;
h = DmQueryRecord(dbP, index);
p = MemHandleLock(h);
if (p->format.bits.isMemo)
{
MemoGetDatabase(&linkDB, dmModeReadWrite);
appNum = 'memo';
}
else if (p->format.bits.isContact)
{
AddrGetDatabase(&linkDB, dmModeReadWrite);
appNum = 'addr';
}
else
{
MemHandleUnlock(h);
DEBUG1("This link is not implemented");
return pgError;
}
if (!linkDB)
{
DEBUG1("Can't open link database");
MemHandleUnlock(h);
return pgError;
}
params = MemPtrNew(sizeof(GoToParamsType));
if (!params)
{
DEBUG1("No more memory for gotoparams");
MemHandleUnlock(h);
DmCloseDatabase(linkDB);
return pgError;
}
params->searchStrLen = 0;
params->dbID = 0;
params->matchPos = params->matchFieldNum = params->matchCustom = 0;
err = DmFindRecordByID(linkDB, p->fields.link.uniqueID, ¶ms->recordNum);
MemHandleUnlock(h);
if (err ||
DmRecordInfo(linkDB, params->recordNum, &attr, NULL, NULL) ||
(attr & dmRecAttrDelete))
{
MemPtrFree(params);
DmCloseDatabase(linkDB);
TaskRemove(dbP, index);
MessageBox(StrLinkNotValid);
FrmUpdateForm(FrmMain, frmRedrawUpdateCode);
return pgError;
}
DmCloseDatabase(linkDB);
MemPtrSetOwner(params, 0);
DmGetNextDatabaseByTypeCreator(true, &searchstate,
'appl', appNum,
true, &cardNo, &appID);
SysUIAppSwitch(0, appID, sysAppLaunchCmdGoTo, params);
return pgOK;
} // pgErr DirectLinkFollow(DmOpenRef dbP, UInt16 index)