Skip to content

Commit

Permalink
Fixing view N click handler (opens O/N) which resolved dvorka#481 and r…
Browse files Browse the repository at this point in the history
…esolves dvorka#479 and resolves dvorka#470.
  • Loading branch information
dvorka committed May 21, 2018
1 parent 9fcb122 commit 59f8dd8
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 12 deletions.
62 changes: 52 additions & 10 deletions app/src/qt/main_window_presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,56 @@ void MainWindowPresenter::showInitialView()
}
}


/* Link handling analysis
*
* PROBLEM:
*
* QWebView RESOLVES clicked link and then delegates it's handling to this
* method. The problem is that this handler does NOT get original link, but
* RESOLVED link - which might be resolved differently than I expected.
* For link resolution is IMPORTANT baseUrl specified within HTML source
* passed to QWebView for rendering.
*
* Input:
*
* Qt URL ... URL resolved by QWebView using a.href and html@baseUrl
* Current O ... link clicked in description of a N from O
* Current N ... link clicked in description of a N from O
*
* Outline link types:
*
* ABSOLUTE link
* - a.href: /home/user/mf/memory/d/o.md
* - Qt URL: file:///home/user/mf/memory/d/o.md
* RELATIVE link SAME directory:
* - a.href: o.md
* - Qt URL: file:///home/user/mf/memory/d/o.md
* HTML.baseUrl + a.href
* RELATIVE link DIFFERENT directory:
* - a.href: ../d/o.md
* - Qt URL: file:///home/user/mf/memory/d/o.md
* HTML.baseUrl + a.href
*
* Note link types
*
* RELATIVE LINKS:
* - a.href: #mangled-note-name
* - Qt URL:
* ... and all O links above w/ #mangled-note-name suffix
*
*/
void MainWindowPresenter::handleNoteViewLinkClicked(const QUrl& url)
{
#ifdef DO_M8R_DEBUG
MF_DEBUG("HTML click handler: " << url.toString().toStdString() << std::endl);
MF_DEBUG("HTML clickHandler: " << endl);
MF_DEBUG(" Qt URL : " << url.toString().toStdString() << endl);
MF_DEBUG(" Memory path: " << config.getMemoryPath() << endl);
MF_DEBUG(" Current O : " << orloj->getOutlineView()->getCurrentOutline()->getKey() << endl);
#endif

statusBar->showInfo(QString(tr("Hyperlink %1 clicked...")).arg(url.toString()));

Outline* currentOutline = orloj->getOutlineView()->getCurrentOutline();
if(url.toString().size()) {
if(url.toString().startsWith("file://")) {
string key{url.toString().toStdString()};
Expand All @@ -186,16 +227,16 @@ void MainWindowPresenter::handleNoteViewLinkClicked(const QUrl& url)
// it CAN be Note

// HANDLE relative N link: #mangled-section-name
// Qt completes relative link w/ base URL which is set to MEMORY path:
// /repository/memory/#mangled-section-name
string relativeLinkPrefix{config.getMemoryPath()};
string currentDir{}, currentFile{};
pathToDirectoryAndFile(currentOutline->getKey(), currentDir, currentFile);
string relativeLinkPrefix{currentDir};
relativeLinkPrefix.append(FILE_PATH_SEPARATOR);
relativeLinkPrefix.append("#");
MF_DEBUG(" Relative prefix: " << relativeLinkPrefix << endl);
if(stringStartsWith(key, relativeLinkPrefix)) {
// it's a relative link within current O
string mangledNoteName = key.substr(offset+1);
MF_DEBUG(" N lookup using: " << mangledNoteName << endl);
MF_DEBUG("HTML clickHandler - N lookup using: " << mangledNoteName << endl);
Outline* o=orloj->getMind()->remind().getOutline(orloj->getOutlineView()->getCurrentOutline()->getKey());
if(o) {
Note* n = o->getNoteByMangledName(mangledNoteName);
Expand All @@ -208,10 +249,10 @@ void MainWindowPresenter::handleNoteViewLinkClicked(const QUrl& url)
statusBar->showInfo(QString(tr("Link target not found for relative link %1")).arg(QString::fromStdString(mangledNoteName)));
return;
} else {
// HANDLE O#N link
// HANDLE O#N link - O can be in a memory SUBDIRECTORY
string mangledNoteName = key.substr(offset+1);
key.erase(offset);
MF_DEBUG(" O lookup using key: " << key << endl);
MF_DEBUG("HTML clickHandler - O lookup using key: " << key << endl);

// IMPROVE find note within outline
Outline* o=orloj->getMind()->remind().getOutline(key);
Expand All @@ -229,10 +270,11 @@ void MainWindowPresenter::handleNoteViewLinkClicked(const QUrl& url)
}
} else {
// it CAN be Outline
MF_DEBUG(" O lookup using key: " << key << std::endl);

// QWebView resolves URL (it is NEVER relative) - use resolved URL as is
MF_DEBUG(" O lookup using path: " << key << std::endl);
Outline* o=orloj->getMind()->remind().getOutline(key);
if(o) {
// Notebook for hyperlink found
orloj->showFacetOutline(o);
return;
} // else fallback to open using desktop services
Expand Down
10 changes: 10 additions & 0 deletions lib/src/gear/file_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ bool isFile(const char* path)
}
}

bool isPathRelative(const string& path)
{
// IMPROVE relative vs. absolute is platform specific (remind Windows; new C++ standards have methods for this)
if(path.size() && path.at(0) == '/') {
return true;
} else {
return false;
}
}

bool createDirectory(const string& path) {
int e = mkdir(path.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
if(e) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/gear/file_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void resolvePath(const std::string& path, std::string& resolvedAbsolutePath);
bool isDirectoryOrFileExists(const char* path);
bool isDirectory(const char* path);
bool isFile(const char* path);
bool isPathRelative(const std::string& path);
char* makeTempDirectory(char* dirNamePefix);
int removeDirectoryRecursively(const char* path);
int copyDirectoryRecursively(const char* srcPath, const char* dstPath);
Expand Down
11 changes: 11 additions & 0 deletions lib/test/resources/links-repository/memory/dst-subdir/links-dst.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Target in sibling subdir <!-- Metadata: type: Outline; created: 2018-05-21 11:19:44; reads: 9; read: 2018-05-21 11:40:57; revision: 9; modified: 2018-05-21 11:40:57; importance: 0/5; urgency: 0/5; -->
Target Outline text.

# N1 <!-- Metadata: type: Note; created: 2018-05-21 11:19:44; reads: 4; read: 2018-05-21 11:40:47; revision: 2; modified: 2018-05-21 11:40:47; -->
Text 1.

# N2 <!-- Metadata: type: Note; created: 2018-05-21 11:19:44; reads: 4; read: 2018-05-21 11:40:52; revision: 2; modified: 2018-05-21 11:40:52; -->
Text 1.

# N3 <!-- Metadata: type: Note; created: 2018-05-21 11:19:44; reads: 3; read: 2018-05-21 11:40:57; revision: 2; modified: 2018-05-21 11:40:57; -->
Text 1.
10 changes: 10 additions & 0 deletions lib/test/resources/links-repository/memory/links-dst.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Target in memory <!-- Metadata: type: Outline; created: 2018-05-21 11:17:46; reads: 13; read: 2018-05-21 11:40:34; revision: 9; modified: 2018-05-21 11:40:34; importance: 0/5; urgency: 0/5; -->
Target Outline text.
# N1 <!-- Metadata: type: Note; created: 2018-05-21 11:17:46; reads: 4; read: 2018-05-21 11:40:24; revision: 2; modified: 2018-05-21 11:40:24; -->
Text 1.

# N2 <!-- Metadata: type: Note; created: 2018-05-21 11:17:46; reads: 4; read: 2018-05-21 11:40:29; revision: 2; modified: 2018-05-21 11:40:29; -->
Text 1.

# N3 <!-- Metadata: type: Note; created: 2018-05-21 11:17:46; reads: 3; read: 2018-05-21 11:40:34; revision: 2; modified: 2018-05-21 11:40:34; -->
Text 1.
47 changes: 47 additions & 0 deletions lib/test/resources/links-repository/memory/links-src.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Links in memory <!-- Metadata: type: Outline; created: 2018-05-10 10:15:30; reads: 249; read: 2018-05-21 11:39:40; revision: 249; modified: 2018-05-21 11:39:40; importance: 0/5; urgency: 0/5; -->
Manual test for **links** with target(s) in:

* the **same** directory
* in **subdirectories**

---

Os links:

* [O: ABSOLUTE O link path](/home/dvorka/p/mindforger/git/mindforger/lib/test/resources/links-repository/memory/links-dst.md)
`/home/dvorka/p/mindforger/git/mindforger/lib/test/resources/links-repository/memory/links-dst.md`
* [O: RELATIVE O link path to SUBDIRECTORY](dst-subdir/links-dst.md)
`dst-subdir/links-dst.md`
* [O: RELATIVE O link to SAME directory](links-dst.md)
`links-dst.md`
* [O: RELATIVE O link to SAME directory](./links-dst.md)
`./links-dst.md`

WIP:

* [O: RELATIVE O link path](~/p/mindforger/git/mindforger/lib/test/resources/links-repository/memory/links-dst.md)
`~/p/mindforger/git/mindforger/lib/test/resources/links-repository/memory/links-dst.md`
**NOT SUPPORTED** by QWebView - most probably it is
intentional because of security & platform specific reasons

---

Ns links:

* [N: ABSOLUTE O link path](/home/dvorka/p/mindforger/git/mindforger/lib/test/resources/links-repository/memory/links-dst.md#n2)
`/home/dvorka/p/mindforger/git/mindforger/lib/test/resources/links-repository/memory/links-dst.md#n2`
* [N: RELATIVE in in this O](#n2) `#n2`
* [N: RELATIVE O link path to SUBDIRECTORY](dst-subdir/links-dst.md#n2)
`dst-subdir/links-dst.md#n2`
* [N: RELATIVE O link to SAME directory](links-dst.md#n2)
`links-dst.md#n2`
* [N: RELATIVE O link to SAME directory](./links-dst.md#n2)
`./links-dst.md#n2`


# N1 <!-- Metadata: type: Note; created: 2018-05-11 12:10:07; reads: 27; read: 2018-05-21 11:23:26; revision: 5; modified: 2018-05-21 11:23:26; -->
N1 text.
# N2 <!-- Metadata: type: Note; created: 2018-05-21 11:22:59; reads: 10; read: 2018-05-21 11:23:19; revision: 3; modified: 2018-05-21 11:23:19; -->
N2 text.
# N3 <!-- Metadata: type: Note; created: 2018-05-21 11:23:07; reads: 4; read: 2018-05-21 11:23:12; revision: 2; modified: 2018-05-21 11:23:12; -->
N3 text.
11 changes: 11 additions & 0 deletions lib/test/resources/links-repository/memory/src-subdir/links-dst.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Target in same subdir <!-- Metadata: type: Outline; created: 2018-05-21 11:19:44; reads: 9; read: 2018-05-21 11:40:57; revision: 9; modified: 2018-05-21 11:40:57; importance: 0/5; urgency: 0/5; -->
Target Outline text.

# N1 <!-- Metadata: type: Note; created: 2018-05-21 11:19:44; reads: 4; read: 2018-05-21 11:40:47; revision: 2; modified: 2018-05-21 11:40:47; -->
Text 1.

# N2 <!-- Metadata: type: Note; created: 2018-05-21 11:19:44; reads: 4; read: 2018-05-21 11:40:52; revision: 2; modified: 2018-05-21 11:40:52; -->
Text 1.

# N3 <!-- Metadata: type: Note; created: 2018-05-21 11:19:44; reads: 3; read: 2018-05-21 11:40:57; revision: 2; modified: 2018-05-21 11:40:57; -->
Text 1.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Links in subdir <!-- Metadata: type: Outline; created: 2018-05-10 10:15:30; reads: 295; read: 2018-05-21 11:47:05; revision: 291; modified: 2018-05-21 11:47:05; importance: 0/5; urgency: 0/5; -->
Manual test for **links** with target(s) in:

* in **parent** directory
* the **sibling** directory

---

Os links:

* [O: RELATIVE O link path to PARENT DIR](../links-dst.md)
`../links-dst.md`
* [O: RELATIVE O link to SAME directory](links-dst.md)
`links-dst.md`
* [O: RELATIVE O link to SAME directory](./links-dst.md)
`./links-dst.md`
* [O: RELATIVE O link path to SIBLING DIR](../dst-subdir/links-dst.md)
`../links-dst.md`

---

Ns links:

* [N: RELATIVE O link path to PARENT DIR](../links-dst.md#n2)
`../links-dst.md#n2`
* [N: RELATIVE O link to SAME directory](links-dst.md#n2)
`links-dst.md#n2`
* [N: RELATIVE O link to SAME directory](./links-dst.md#n2)
`./links-dst.md#n2`
* [N: RELATIVE O link path to SIBLING DIR](../dst-subdir/links-dst.md#n2)
`../links-dst.md#n2`
# N1 <!-- Metadata: type: Note; created: 2018-05-11 12:10:07; reads: 27; read: 2018-05-21 11:23:26; revision: 5; modified: 2018-05-21 11:23:26; -->
N1 text.
# N2 <!-- Metadata: type: Note; created: 2018-05-21 11:22:59; reads: 10; read: 2018-05-21 11:23:19; revision: 3; modified: 2018-05-21 11:23:19; -->
N2 text.
# N3 <!-- Metadata: type: Note; created: 2018-05-21 11:23:07; reads: 4; read: 2018-05-21 11:23:12; revision: 2; modified: 2018-05-21 11:23:12; -->
N3 text.
4 changes: 2 additions & 2 deletions mindforger.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.0.1, 2018-05-18T16:32:01. -->
<!-- Written by QtCreator 3.0.1, 2018-05-21T12:20:24. -->
<qtcreator>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
Expand Down Expand Up @@ -171,7 +171,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">app</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:/home/dvorka/p/mindforger/git/mindforger/app/app.pro</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments">~/tmp/small-repository</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments">/home/dvorka/p/mindforger/git/mindforger/lib/test/resources/links-repository</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">app/app.pro</value>
<value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix">false</value>
<value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseTerminal">false</value>
Expand Down

0 comments on commit 59f8dd8

Please sign in to comment.