Question
Programmatically access link information in C++ plugin
Hello,
I would like to access the links in the link list in my C++ plugin.

I need the path to the linked file and the page number.
I came up with some code that works, but it seems a bit cumbersome to me and i am not sure if this is the right approach.
static void logAllLinks(const IDocument* const doc)
{
IDataBase* db = ::GetDataBase(doc);
LinkQuery query;
InterfacePtr<ILinkManager> linkMgr(db, db->GetRootUID(), UseDefaultIID());
InterfacePtr<IPageList> pages(doc, UseDefaultIID());
UIDList uids(db);
const int32 cnt = linkMgr->QueryLinks(query, kIDLinkClientID, uids);
for (int32 i = 0; i < cnt; ++i)
{
UIDRef linkRef(db, uids[i]);
InterfacePtr<ILink> link(linkRef, UseDefaultIID());
UID resUID = link->GetResource();
InterfacePtr<ILinkResource> linkResource(db, resUID, UseDefaultIID());
// get link name (path)
PMString linkName = linkResource->GetLongName(false);
// get modification date
IDTime modtime = linkResource->GetModTime();
WideString timeString;
bool success = modtime.DateToString(timeString, false);
// get frame
UID frameUID = link->GetObject();
UIDRef frameRef = UIDRef(db, frameUID);
InterfacePtr<IHierarchy> hierarchy(frameRef, UseDefaultIID());
// get parent
UID parentUID = hierarchy->GetParentUID();
UIDRef parentFrameRef = UIDRef(db, parentUID);
// get page
UID pageUID = Utils<ILayoutUtils>()->GetOwnerPageUID(hierarchy, kTrue);
int32 pageNum = pages->GetPageIndex(pageUID);
pageNum += 1; // 0 - based
AppGlobals::Instance().getLogger()->log("logAllLinks: Found link: [frame: %d] [parent frame: %d] [name: %s] [page: %d] [modified: %s]", Logger::E_LOG_LEVEL::ELL_DEBUG,
frameRef.GetUID().Get(), parentFrameRef.GetUID().Get(), Helper::stringWithPMString(linkName).c_str(), pageNum, Helper::stringWithPMString(timeString).c_str());
}
}
Is there an easier way to do this and are there any pitfalls I need to be aware of?
Thanks
