diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-12-18 22:05:22 +0200 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-12-18 22:05:22 +0200 |
commit | 2d393d51772bfea81e704df9f40475ce94dfec9d (patch) | |
tree | 8f747b96e88eeeda67c7f2d18a68218a11b729ce /src/ui/documentwidget.c | |
parent | 21669124f2600376b8ec12d0710c56b1838bff0c (diff) |
Import all links as bookmarks
Diffstat (limited to 'src/ui/documentwidget.c')
-rw-r--r-- | src/ui/documentwidget.c | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index 3a03dce6..22fa29f8 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c | |||
@@ -20,10 +20,14 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||
20 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 20 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
21 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | 21 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
22 | 22 | ||
23 | /* TODO: This file is a little too large. DocumentWidget could be split into | ||
24 | a couple of smaller objects. One for rendering the document, for instance. */ | ||
25 | |||
23 | #include "documentwidget.h" | 26 | #include "documentwidget.h" |
24 | 27 | ||
25 | #include "app.h" | 28 | #include "app.h" |
26 | #include "audio/player.h" | 29 | #include "audio/player.h" |
30 | #include "bookmarks.h" | ||
27 | #include "command.h" | 31 | #include "command.h" |
28 | #include "defs.h" | 32 | #include "defs.h" |
29 | #include "gmcerts.h" | 33 | #include "gmcerts.h" |
@@ -1470,6 +1474,13 @@ static void saveToDownloads_(const iString *url, const iString *mime, const iBlo | |||
1470 | delete_String(savePath); | 1474 | delete_String(savePath); |
1471 | } | 1475 | } |
1472 | 1476 | ||
1477 | static void addAllLinks_(void *context, const iGmRun *run) { | ||
1478 | iPtrArray *links = context; | ||
1479 | if (~run->flags & decoration_GmRunFlag && run->linkId) { | ||
1480 | pushBack_PtrArray(links, run); | ||
1481 | } | ||
1482 | } | ||
1483 | |||
1473 | static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd) { | 1484 | static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd) { |
1474 | iWidget *w = as_Widget(d); | 1485 | iWidget *w = as_Widget(d); |
1475 | if (equal_Command(cmd, "window.resized") || equal_Command(cmd, "font.changed")) { | 1486 | if (equal_Command(cmd, "window.resized") || equal_Command(cmd, "font.changed")) { |
@@ -1872,6 +1883,46 @@ static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd) | |||
1872 | } | 1883 | } |
1873 | return iTrue; | 1884 | return iTrue; |
1874 | } | 1885 | } |
1886 | else if (equal_Command(cmd, "bookmark.links") && document_App() == d) { | ||
1887 | iPtrArray *links = collectNew_PtrArray(); | ||
1888 | render_GmDocument(d->doc, (iRangei){ 0, size_GmDocument(d->doc).y }, addAllLinks_, links); | ||
1889 | /* Find links that aren't already bookmarked. */ | ||
1890 | iForEach(PtrArray, i, links) { | ||
1891 | const iGmRun *run = i.ptr; | ||
1892 | if (findUrl_Bookmarks(bookmarks_App(), linkUrl_GmDocument(d->doc, run->linkId))) { | ||
1893 | remove_PtrArrayIterator(&i); | ||
1894 | } | ||
1895 | } | ||
1896 | if (!isEmpty_PtrArray(links)) { | ||
1897 | if (argLabel_Command(cmd, "confirm")) { | ||
1898 | const char *plural = size_PtrArray(links) != 1 ? "s" : ""; | ||
1899 | makeQuestion_Widget( | ||
1900 | uiHeading_ColorEscape "IMPORT BOOKMARKS", | ||
1901 | format_CStr("Found %d new link%s on the page.", size_PtrArray(links), plural), | ||
1902 | (const char *[]){ "Cancel", | ||
1903 | format_CStr(uiTextAction_ColorEscape "Add %d Bookmark%s", | ||
1904 | size_PtrArray(links), plural) }, | ||
1905 | (const char *[]){ "cancel", "bookmark.links" }, | ||
1906 | 2); | ||
1907 | } | ||
1908 | else { | ||
1909 | iConstForEach(PtrArray, j, links) { | ||
1910 | const iGmRun *run = j.ptr; | ||
1911 | add_Bookmarks(bookmarks_App(), | ||
1912 | linkUrl_GmDocument(d->doc, run->linkId), | ||
1913 | collect_String(newRange_String(run->text)), | ||
1914 | NULL, | ||
1915 | 0x1f588 /* pin */); | ||
1916 | } | ||
1917 | postCommand_App("bookmarks.changed"); | ||
1918 | } | ||
1919 | } | ||
1920 | else { | ||
1921 | makeMessage_Widget(uiHeading_ColorEscape "IMPORT BOOKMARKS", | ||
1922 | "All links on this page are already bookmarked."); | ||
1923 | } | ||
1924 | return iTrue; | ||
1925 | } | ||
1875 | return iFalse; | 1926 | return iFalse; |
1876 | } | 1927 | } |
1877 | 1928 | ||
@@ -2313,8 +2364,10 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e | |||
2313 | { "Bookmark Page...", SDLK_d, KMOD_PRIMARY, "bookmark.add" }, | 2364 | { "Bookmark Page...", SDLK_d, KMOD_PRIMARY, "bookmark.add" }, |
2314 | { "Subscribe to Page...", subscribeToPage_KeyModifier, "feeds.subscribe" }, | 2365 | { "Subscribe to Page...", subscribeToPage_KeyModifier, "feeds.subscribe" }, |
2315 | { "---", 0, 0, NULL }, | 2366 | { "---", 0, 0, NULL }, |
2367 | { "Import Links as Bookmarks...", 0, 0, "bookmark.links confirm:1" }, | ||
2368 | { "---", 0, 0, NULL }, | ||
2316 | { "Copy Page URL", 0, 0, "document.copylink" } }, | 2369 | { "Copy Page URL", 0, 0, "document.copylink" } }, |
2317 | 11); | 2370 | 13); |
2318 | if (isEmpty_Range(&d->selectMark)) { | 2371 | if (isEmpty_Range(&d->selectMark)) { |
2319 | pushBackN_Array( | 2372 | pushBackN_Array( |
2320 | &items, | 2373 | &items, |