From 2ed60860b2c816578837b0167ab032596a7df4e4 Mon Sep 17 00:00:00 2001 From: Gregor Rosenauer Date: Tue, 13 Aug 2024 17:49:23 +0200 Subject: [PATCH 1/5] fixes #116 and jumps to page if provided via argument. --- bepdf/beos/BepdfApplication.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bepdf/beos/BepdfApplication.cpp b/bepdf/beos/BepdfApplication.cpp index 428aee5..39a9676 100644 --- a/bepdf/beos/BepdfApplication.cpp +++ b/bepdf/beos/BepdfApplication.cpp @@ -545,12 +545,21 @@ void BepdfApplication::RefsReceived ( BMessage * msg ) BString ownerPassword, userPassword; const char *owner = NULL; const char *user = NULL; + int32 pageNum = 0; + if (B_OK == msg->FindString("ownerPassword", &ownerPassword)) { owner = ownerPassword.String(); } if (B_OK == msg->FindString("userPassword", &userPassword)) { user = userPassword.String(); } + status_t result = msg->FindInt32(PAGE_NUM_MSG, &pageNum); + if (result != B_OK) { + if (result != B_NAME_NOT_FOUND) { + BAlert *error = new BAlert(B_TRANSLATE("Error"), B_TRANSLATE("BePDF: Error getting page number!"), B_TRANSLATE("Close"), NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); + error->Go(); + } + } Initialize(); @@ -589,6 +598,12 @@ void BepdfApplication::RefsReceived ( BMessage * msg ) mWindow = win; win->Show(); } + // jump to page if provided + if (pageNum != 0) { + mWindow->LockLooper(); + mWindow->SetPage(pageNum); + mWindow->UnlockLooper(); + } // stop after first document mGotSomething = true; break; From 70b2cef03bfee17719b24d2e7adf82cfac21cd49 Mon Sep 17 00:00:00 2001 From: Gregor Rosenauer Date: Wed, 14 Aug 2024 17:54:15 +0200 Subject: [PATCH 2/5] harden and fix page number handling. --- bepdf/beos/BepdfApplication.cpp | 30 +++++++++++++++++------------- bepdf/beos/PDFWindow.cpp | 6 +++++- bepdf/beos/PDFWindow.h | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/bepdf/beos/BepdfApplication.cpp b/bepdf/beos/BepdfApplication.cpp index 39a9676..15a8f1b 100644 --- a/bepdf/beos/BepdfApplication.cpp +++ b/bepdf/beos/BepdfApplication.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -60,7 +61,7 @@ static const char * GPLCopyright = "\n\n" "This program is free software under the GNU GPL v2, or any later version.\n"; -static const char *PAGE_NUM_MSG = "bepdf:page_num"; +static const char *PAGE_NUM_MSG_KEY = "bepdf:page_num"; static const char *settingsFilename = "BePDF"; @@ -528,24 +529,27 @@ bool BepdfApplication::QuitRequested() { /* Opens everything. */ -void BepdfApplication::RefsReceived ( BMessage * msg ) +void BepdfApplication::RefsReceived(BMessage *msg) { uint32 type; int32 count; - int32 i; - entry_ref ref; - mReadyToQuit = false; + status_t result; - msg->GetInfo ( "refs", &type, &count ); - if ( type != B_REF_TYPE ) { - return; - } + msg->GetInfo("refs", &type, &count); + + if (type != B_REF_TYPE) { + BAlert *error = new BAlert(B_TRANSLATE("Error"), B_TRANSLATE("Invalid file reference received!"), B_TRANSLATE("Close"), NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); + error->Go(); + + return; + } BString ownerPassword, userPassword; const char *owner = NULL; const char *user = NULL; int32 pageNum = 0; + entry_ref ref; if (B_OK == msg->FindString("ownerPassword", &ownerPassword)) { owner = ownerPassword.String(); @@ -553,17 +557,17 @@ void BepdfApplication::RefsReceived ( BMessage * msg ) if (B_OK == msg->FindString("userPassword", &userPassword)) { user = userPassword.String(); } - status_t result = msg->FindInt32(PAGE_NUM_MSG, &pageNum); + result = msg->FindInt32(PAGE_NUM_MSG_KEY, &pageNum); if (result != B_OK) { if (result != B_NAME_NOT_FOUND) { - BAlert *error = new BAlert(B_TRANSLATE("Error"), B_TRANSLATE("BePDF: Error getting page number!"), B_TRANSLATE("Close"), NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); + BAlert *error = new BAlert(B_TRANSLATE("Error"), B_TRANSLATE("Error getting page number!"), B_TRANSLATE("Close"), NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); error->Go(); } } Initialize(); - for ( i = --count ; i >= 0; i-- ) { + for (int32 i = --count ; i >= 0; i-- ) { if ( msg->FindRef("refs", i, &ref ) == B_OK ) { /* Open the document... @@ -688,7 +692,7 @@ BepdfApplication::ArgvReceived (int32 argc, char **argv) // fprintf(errFile, "%s%s\n", pdfViewerCopyright, GPLCopyright); BMessage msg(B_REFS_RECEIVED); - msg.AddInt32 (PAGE_NUM_MSG, pg); + msg.AddInt32 (PAGE_NUM_MSG_KEY, pg); get_ref_for_path (argvCopy[1], &fileToOpen); msg.AddRef ("refs", &fileToOpen); PostMessage (&msg); diff --git a/bepdf/beos/PDFWindow.cpp b/bepdf/beos/PDFWindow.cpp index e12832b..4abbddd 100644 --- a/bepdf/beos/PDFWindow.cpp +++ b/bepdf/beos/PDFWindow.cpp @@ -973,8 +973,12 @@ PDFWindow::SetZoomSize(float w, float h) /////////////////////////////////////////////////////////// // update page list and page number item void -PDFWindow::SetPage(int16 page) { +PDFWindow::SetPage(int32 page) { char pageStr [64]; + if (page <= 0) page = 1; + if (page > mPagesView->CountItems()) { + page = mPagesView->CountItems(); + } snprintf (pageStr, sizeof (pageStr), "%d", page); mPageNumberItem->SetText (pageStr); mPagesView->Select(page-1); diff --git a/bepdf/beos/PDFWindow.h b/bepdf/beos/PDFWindow.h index e9d30d2..49e1737 100644 --- a/bepdf/beos/PDFWindow.h +++ b/bepdf/beos/PDFWindow.h @@ -283,7 +283,7 @@ class PDFWindow void SetZoomSize (float w, float h); void SetZoom(int16 zoom); void SetRotation(float rotation); - void SetPage(int16 page); + void SetPage(int32 page); static void OpenPDF(const char* file); static bool OpenPDFHelp(const char* name); From 5f519e9a8bbbb16b2a5cb7bacf66cd6180dd82d8 Mon Sep 17 00:00:00 2001 From: Gregor Rosenauer Date: Tue, 27 Aug 2024 12:59:50 +0200 Subject: [PATCH 3/5] fix handling of encryption wrt errors opening PDF file. --- bepdf/beos/BepdfApplication.cpp | 14 +++++++------- bepdf/beos/PDFView.cpp | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bepdf/beos/BepdfApplication.cpp b/bepdf/beos/BepdfApplication.cpp index 428aee5..82aabd5 100644 --- a/bepdf/beos/BepdfApplication.cpp +++ b/bepdf/beos/BepdfApplication.cpp @@ -564,6 +564,7 @@ void BepdfApplication::RefsReceived ( BMessage * msg ) BRect rect(mSettings->GetWindowRect()); bool ok; bool encrypted = false; + if (mWindow == NULL) { win = new PDFWindow(&ref, rect, owner, user, &encrypted); ok = win->IsOk(); @@ -578,19 +579,18 @@ void BepdfApplication::RefsReceived ( BMessage * msg ) if (!encrypted) { BAlert *error = new BAlert(B_TRANSLATE("Error"), B_TRANSLATE("BePDF: Error opening file!"), B_TRANSLATE("Close"), NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT); error->Go(); - } - + OpenFilePanel(); + } else { + new PasswordWindow(&ref, rect, this); + } if (mWindow == NULL) delete win; - if (encrypted) { - new PasswordWindow(&ref, rect, this); - } } else if (mWindow == NULL) { mWindow = win; win->Show(); } - // stop after first document - mGotSomething = true; + // stop after first document + mGotSomething = true; break; } } diff --git a/bepdf/beos/PDFView.cpp b/bepdf/beos/PDFView.cpp index f969d98..da295b6 100644 --- a/bepdf/beos/PDFView.cpp +++ b/bepdf/beos/PDFView.cpp @@ -232,6 +232,9 @@ PDFView::MakeTitleString(BPath* path) { bool PDFView::OpenFile(entry_ref *ref, const char *ownerPassword, const char *userPassword, bool *encrypted) { BEntry entry (ref, true); + if (!entry.Exists()) { + return false; + } BPath path; entry.GetPath (&path); @@ -245,11 +248,8 @@ PDFView::OpenFile(entry_ref *ref, const char *ownerPassword, const char *userPas UpdatePanelDirectory(&path); bool ok = newDoc->isOk(); - // xpdf 3.01 returns false even PDF file is password protected?!? - *encrypted = true; // newDoc->isEncrypted(); -// fprintf(stderr, "ok %s encrypted %s\n", -// ok ? "yes" : "no", -// (*encrypted) ? "yes" : "no"); + *encrypted = newDoc->isEncrypted(); + if (ok) { delete mDoc; mDoc = newDoc; From b3fba34b4b99e36de6cb5eddace77604b5c769e0 Mon Sep 17 00:00:00 2001 From: Gregor Rosenauer Date: Tue, 27 Aug 2024 20:40:34 +0200 Subject: [PATCH 4/5] open file panel only when there is no open window yet. --- bepdf/beos/BepdfApplication.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bepdf/beos/BepdfApplication.cpp b/bepdf/beos/BepdfApplication.cpp index 82aabd5..16c20b0 100644 --- a/bepdf/beos/BepdfApplication.cpp +++ b/bepdf/beos/BepdfApplication.cpp @@ -579,7 +579,10 @@ void BepdfApplication::RefsReceived ( BMessage * msg ) if (!encrypted) { BAlert *error = new BAlert(B_TRANSLATE("Error"), B_TRANSLATE("BePDF: Error opening file!"), B_TRANSLATE("Close"), NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT); error->Go(); - OpenFilePanel(); + + if (mWindow == NULL) { // fixme: always true even if a PDF window is already open! + OpenFilePanel(); + } } else { new PasswordWindow(&ref, rect, this); } From 446ba432b55aa136bff54b7f8d213ff448c8be99 Mon Sep 17 00:00:00 2001 From: Gregor Rosenauer Date: Wed, 28 Aug 2024 11:37:30 +0200 Subject: [PATCH 5/5] fixup for pagenum patch to reuse existing method invoked from TextView and correctly update pagelist selection and position. --- bepdf/beos/BepdfApplication.cpp | 6 ++--- bepdf/beos/PDFWindow.cpp | 43 ++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/bepdf/beos/BepdfApplication.cpp b/bepdf/beos/BepdfApplication.cpp index 15a8f1b..ad184fa 100644 --- a/bepdf/beos/BepdfApplication.cpp +++ b/bepdf/beos/BepdfApplication.cpp @@ -604,9 +604,9 @@ void BepdfApplication::RefsReceived(BMessage *msg) } // jump to page if provided if (pageNum != 0) { - mWindow->LockLooper(); - mWindow->SetPage(pageNum); - mWindow->UnlockLooper(); + BMessage goToPageMsg(PDFWindow::GOTO_PAGE_CMD); + goToPageMsg.AddInt32("page", pageNum); + mWindow->MessageReceived(&goToPageMsg); } // stop after first document mGotSomething = true; diff --git a/bepdf/beos/PDFWindow.cpp b/bepdf/beos/PDFWindow.cpp index 4abbddd..35b5c2a 100644 --- a/bepdf/beos/PDFWindow.cpp +++ b/bepdf/beos/PDFWindow.cpp @@ -975,10 +975,6 @@ PDFWindow::SetZoomSize(float w, float h) void PDFWindow::SetPage(int32 page) { char pageStr [64]; - if (page <= 0) page = 1; - if (page > mPagesView->CountItems()) { - page = mPagesView->CountItems(); - } snprintf (pageStr, sizeof (pageStr), "%d", page); mPageNumberItem->SetText (pageStr); mPagesView->Select(page-1); @@ -1069,22 +1065,31 @@ PDFWindow::MessageReceived(BMessage* message) mMainView->MoveToPage (mMainView->GetNumPages()); break; case GOTO_PAGE_CMD: { - status_t err; - BTextControl * control; - BControl * ptr; - - err = message->FindPointer ("source", (void **)&ptr); - control = dynamic_cast (ptr); - if (err == B_OK && control != NULL) { - const char *txt = control->Text (); - page = atoi (txt); - mMainView->MoveToPage (page); - mMainView->MakeFocus(); - } else { - /* ERROR */ - } - } + status_t result; + BTextControl * control; + BControl * ptr; + + result = message->FindPointer ("source", (void **)&ptr); + if (result == B_OK) { + control = dynamic_cast (ptr); + if (result == B_OK && control != NULL) { + const char *txt = control->Text (); + page = atoi (txt); + } + } else { // may come from external source over page parameter + result = message->FindInt32("page", &page); + if (result == B_OK) + mMainView->WaitForPage(); + } + + if (result == B_OK) { + LockLooper(); + mMainView->MoveToPage (page); + UnlockLooper(); + mMainView->MakeFocus(); + } break; + } case PAGE_SELECTED_CMD: page = mPagesView->CurrentSelection(0) + 1; mMainView->MoveToPage(page);