📄 linkdemo.cpp
字号:
//===========================================================================
//
// Module: LINKDEMO.CPP
//
// Description:
// Sample C++ API program that demonstrates the use of the
// rich text classes:
//
// LNLinkHotspot
// LNDatabaseLink
// LNViewLink
// LNDocumentLink
//
// Syntax: LINKDEMO
//
//===========================================================================
#include <iostream>
#include <stdio.h>
// Notes C++ API headers.
#include "lncppapi.h"
#include "editdflt.h"
#include "lnlinks.hpp"
using namespace std;
char CommandBuf[80];
void ProcessLinks( LNDocument *SrcDoc, LNDocument *DestDoc,
LNDatabaseLink *SrcDBLink, LNDatabaseLink *DestDBLink );
LNBOOL SearchDocument( const LNDocument &Doc,
const LNString &FieldName,
const LNString &SearchStr );
void PrintTimeDate( TIMEDATE *td );
void PrintUNID( UNID *unid );
//---------------------------------------------------------------------------
//
// MAIN
//
//---------------------------------------------------------------------------
int main( int argc, char *argv[] )
{
int ProgramStatus = 0;
LNNotesSession Session;
// Throw all errors encountered during command execution.
LNSetThrowAllErrors (TRUE);
try
{
LNDatabase SrcDB;
LNDatabase DestDB;
LNDocumentArray DocArray;
LNDocument SrcDoc;
LNDocument DestDoc;
LNINT Index;
LNINT Count;
LNBOOL IsDocFound;
// Initialize the API.
Session.Init (argc, argv);
// Get the source Notes database.
Session.GetDatabase( "linkdemo.nsf", &SrcDB );
// Get the destination Notes database.
Session.GetDatabase( "rtsample.nsf", &DestDB );
// Open the source database.
SrcDB.Open();
// Get an array of all the docs in the database.
SrcDB.GetDocuments( &DocArray );
// Search all the documents in the array for the
// source document we are to test against.
IsDocFound = FALSE;
Count = DocArray.GetCount();
for (Index = 0; Index<Count; Index++)
{
// Get the next document in the array.
SrcDoc = DocArray[Index];
// Open it.
SrcDoc.Open( LNNOTEOPENFLAGS_DEFAULT );
if ( SrcDoc.HasItem("Body") )
{
// This document has a body field, so search for the string in it.
if ( SearchDocument( SrcDoc, "Body", "*Here are some links for testing*" ) )
{
// Document was found; leave the loop now without closing the doc.
IsDocFound = TRUE;
break;
}
}
SrcDoc.Close();
}
if (!IsDocFound)
throw ("Could not find the source document needed for this demo!");
// Open the destination database.
DestDB.Open();
// Get an array of all the docs in the database.
DestDB.GetDocuments( &DocArray );
// Search all the documents in the array for the
// destination document we are to test against.
IsDocFound = FALSE;
Count = DocArray.GetCount();
for (Index = 0; Index<Count; Index++)
{
// Get the next document in the array.
DestDoc = DocArray[Index];
// Open it.
DestDoc.Open( LNNOTEOPENFLAGS_DEFAULT );
if ( DestDoc.HasItem("Body") )
{
// This document has a body field, so search for the string in it.
if ( SearchDocument( DestDoc, "Body", " *END LINKDEMO*" ) )
{
// Document was found; leave the loop now without closing the doc.
IsDocFound = TRUE;
break;
}
}
DestDoc.Close();
}
if (!IsDocFound)
throw ("Could not find the Destination document needed for this demo!");
// Make links to both the source and dest databases.
LNDatabaseLink SrcDBLink( SrcDB );
LNDatabaseLink DestDBLink( DestDB );
// String was found; now process all the links you can find
// or create (the meat of this sample).
ProcessLinks( &SrcDoc, &DestDoc, &SrcDBLink, &DestDBLink );
// Close the documents and databases.
SrcDoc.Close();
DestDoc.Close();
SrcDB.Close();
DestDB.Close();
ProgramStatus = 0;
} // END try
catch (LNSTATUS lnerror)
{
char ErrorBuf[LNERROR_MESSAGE_LENGTH];
LNGetErrorMessage(lnerror, ErrorBuf, LNERROR_MESSAGE_LENGTH);
cout << "\nError: " << ErrorBuf << endl;
ProgramStatus = 2;
}
catch (const char *pErrorMessage)
{
cout << "\nError: " << pErrorMessage << endl << endl;
ProgramStatus = 1;
}
cout << "All Done. Hit return to exit: ";
cin.getline(CommandBuf, 50);
// Terminate the API.
Session.Term();
return (ProgramStatus);
} // END MAIN
//---------------------------------------------------------------------------
//
// Name:
// SearchDocument
//
// Description:
// Searches stylized text elements in a document's rich text field
// for the presence of a search string and returns TRUE if found.
//---------------------------------------------------------------------------
LNBOOL SearchDocument( const LNDocument &Doc,
const LNString &FieldName,
const LNString &SearchStr )
{
LNRichText RT;
LNRTCursor Cursor;
// Get the document's body field as a rich text item.
Doc.GetItem( FieldName, &RT );
// Get the position of the very beginning of the RT field.
RT.GetCursor( &Cursor );
// Find the first stylized text in the richtext.
if ( Cursor.GotoFirst(SearchStr) == LNWARN_NOT_FOUND )
return FALSE;
return TRUE;
} // END SearchDocument()
//---------------------------------------------------------------------------
//
// Name:
// ProcessLinks
//
// Description:
//
//---------------------------------------------------------------------------
void ProcessLinks( LNDocument *SrcDoc, LNDocument *DestDoc,
LNDatabaseLink *SrcDBLink, LNDatabaseLink *DestDBLink )
{
LNSTATUS error;
LNRichText SrcRT;
LNRichText DestRT;
LNRTCursor SrcCursor;
LNRTCursor DestCursor;
LNLinkHotspot linkhotspot;
LNDatabaseLink DBLink;
LNDocumentLink DocLink;
LNViewLink ViewLink;
LNFontStyle fontstyle;
TIMEDATE *TimeDate;
UNID *Unid;
LNBOOL IsLink = FALSE;
// Get the rich text "Body" field from the source document.
cout << endl << "Getting the \"Body\" rich text field from the source document." << endl;
SrcDoc->GetItem("Body", &SrcRT);
// Get a start cursor for this RT object.
SrcRT.GetCursor( &SrcCursor );
// Get and set stuff in our newly created link to the destination database.
DestDBLink->SetDescription( "LINKDEMO Destination Database Link");
TimeDate = DestDBLink->GetLinkedDatabaseReplicaID();
// Find the first hotspot in the richtext. This should be the place
// In the document where the word: "Habenero" appears.
error = SrcCursor.GotoFirst(LNRTTYPE_LINK_HOTSPOT, &linkhotspot);
// throw any warnings here too. (i.e. LNWARN_NOT_FOUND) to test for SPR remove this later ***dhs
if (error)
throw error;
// Get an end cursor for the hot spot. Both cursors will be used
// to modify the font style of the text in the hot spot.
DestCursor.GotoEnd( linkhotspot );
// Set the Destination database link for the hot spot.
linkhotspot.SetLink( *DestDBLink );
// Get the font style at the beginning of the hot spot.
SrcCursor.GetFontStyle( &fontstyle );
// Change font color to red.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -