diff --git a/TODO.txt b/TODO.txt index a50b9d2..9c4673b 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,5 +1,4 @@ Important Features: -* Open As support * Show download progress/failures in FeedView's feedlist * Update FeedView's feedlist when feeds added/removed * Show in desktray diff --git a/src/App.cpp b/src/App.cpp index 0ed09a3..8729df0 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -28,6 +28,7 @@ int main(int argc, char** argv) { + srand(time(0)); installMimeTypes(); App* app = new App(); @@ -113,7 +114,6 @@ App::ArgvReceived(int32 argc, char** argv) for (int i = 1; i < argc; i++) { entry_ref ref; BEntry entry(argv[i]); - std::cout << "ARGV" << std::endl; if (entry.Exists() && entry.GetRef(&ref) == B_OK) { refMsg.AddRef("refs", &ref); @@ -141,7 +141,7 @@ App::RefsReceived(BMessage* message) char type[B_FILE_NAME_LENGTH]; while (message->HasRef("refs", i)) { - BMessage msg = BMessage(B_REFS_RECEIVED); + BMessage msg(B_REFS_RECEIVED); message->FindRef("refs", i++, &ref); msg.AddRef("refs", &ref); @@ -160,14 +160,50 @@ App::RefsReceived(BMessage* message) void App::_OpenEntryFile(BMessage* refMessage) { - const char* openWith = fPreferences->EntryOpenWith(); - entry_ref openRef; + entry_ref entryRef; + refMessage->FindRef("refs", &entryRef); + if (fPreferences->EntryOpenAsHtml()) + _OpenEntryFileAsHtml(entryRef); + else + _OpenEntryFileAsUrl(entryRef); +} + + +void +App::_OpenEntryFileAsHtml(entry_ref ref) +{ + const char* openWith = fPreferences->EntryOpenWith(); + entry_ref openWithRef; + BString entryTitle("untitled"); + BFile(&ref, B_READ_ONLY).ReadAttrString("Feed:name", &entryTitle); + + entry_ref tempRef = tempHtmlFile(&ref, entryTitle.String()); + BMessage newRefMessage(B_REFS_RECEIVED); + newRefMessage.AddRef("refs", &tempRef); if (BMimeType(openWith).IsValid()) - BRoster().Launch(openWith, refMessage); - else if (BEntry(openWith).GetRef(&openRef) == B_OK) - BRoster().Launch(&openRef, refMessage); + BRoster().Launch(openWith, &newRefMessage); + else if (BEntry(openWith).GetRef(&openWithRef) == B_OK) + BRoster().Launch(&openWithRef, &newRefMessage); +} + + +void +App::_OpenEntryFileAsUrl(entry_ref ref) +{ + const char* openWith = fPreferences->EntryOpenWith(); + entry_ref openWithRef; + BString entryUrl; + if (BFile(&ref, B_READ_ONLY).ReadAttrString("META:url", &entryUrl) != B_OK) + return; + + const char* urlArg = entryUrl.String(); + + if (BMimeType(openWith).IsValid()) + BRoster().Launch(openWith, 1, &urlArg); + else if (BEntry(openWith).GetRef(&openWithRef) == B_OK) + BRoster().Launch(&openWithRef, 1, &urlArg); } diff --git a/src/App.h b/src/App.h index e393bcb..80139a3 100644 --- a/src/App.h +++ b/src/App.h @@ -32,6 +32,8 @@ public: private: void _OpenEntryFile(BMessage* refMessage); + void _OpenEntryFileAsHtml(entry_ref ref); + void _OpenEntryFileAsUrl(entry_ref ref); void _OpenSourceFile(BMessage* refMessage); FeedController* fFeedController; diff --git a/src/EntriesView.cpp b/src/EntriesView.cpp index 32af875..c8fdd8d 100644 --- a/src/EntriesView.cpp +++ b/src/EntriesView.cpp @@ -35,7 +35,6 @@ EntriesView::AttachedToWindow() fEntryFolderText->SetTarget(this); fEntryFolderBrowseButton->SetTarget(this); - fOpenAsAutoRadio->SetTarget(this); fOpenAsHtmlRadio->SetTarget(this); fOpenAsUrlRadio->SetTarget(this); fOpenWithSelectButton->SetTarget(this); @@ -65,11 +64,6 @@ EntriesView::MessageReceived(BMessage* msg) ((App*)be_app)->fPreferences->fOpenAs = kOpenAsUrl; break; } - case kOpenAutoRadio: - { - ((App*)be_app)->fPreferences->fOpenAs = kOpenAsAuto; - break; - } case kOpenWithSelect: { ((App*)be_app)->fPreferences->SetEntryOpenWith( @@ -124,8 +118,6 @@ EntriesView::_InitInterface() fOpeningBox->SetLabel("Opening"); fOpenAsLabel = new BStringView("openAsLabel", "Open as:"); - fOpenAsAutoRadio = new BRadioButton("asAuto", "Auto", - new BMessage(kOpenAutoRadio)); fOpenAsHtmlRadio = new BRadioButton("asHtml", "HTML", new BMessage(kOpenHtmlRadio)); fOpenAsUrlRadio = new BRadioButton("asUrl", "URL", @@ -142,10 +134,8 @@ EntriesView::_InitInterface() Preferences* prefs = ((App*)be_app)->fPreferences; if (prefs->fOpenAs == kOpenAsHtml) fOpenAsHtmlRadio->SetValue(B_CONTROL_ON); - else if (prefs->fOpenAs == kOpenAsUrl) + else fOpenAsUrlRadio->SetValue(B_CONTROL_ON); - else - fOpenAsAutoRadio->SetValue(B_CONTROL_ON); fEntryFolderText->SetText(prefs->EntryDir()); @@ -177,7 +167,6 @@ EntriesView::_InitInterface() .AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING) .SetInsets(0, 20, B_USE_ITEM_INSETS, 0) .AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING) - .Add(fOpenAsAutoRadio) .Add(fOpenAsHtmlRadio) .Add(fOpenAsUrlRadio) .AddGlue() diff --git a/src/EntriesView.h b/src/EntriesView.h index ca6c1c3..34a8e91 100644 --- a/src/EntriesView.h +++ b/src/EntriesView.h @@ -25,7 +25,6 @@ enum kEntryFolderBrowse = 'tbef', kOpenHtmlRadio = 'rdow', kOpenUrlRadio = 'roow', - kOpenAutoRadio = 'raow', kOpenWithSelect = 'mnow', kOpenWithPath = 'pbow', kOpenWithBrowse = 'tbow' @@ -51,7 +50,6 @@ private: BBox* fOpeningBox; BStringView* fOpenAsLabel; - BRadioButton* fOpenAsAutoRadio; BRadioButton* fOpenAsHtmlRadio; BRadioButton* fOpenAsUrlRadio; BStringView* fOpenWithLabel; diff --git a/src/Preferences.cpp b/src/Preferences.cpp index 02eeffe..2c1e4b6 100644 --- a/src/Preferences.cpp +++ b/src/Preferences.cpp @@ -143,3 +143,21 @@ Preferences::SetEntryOpenWith(const char* binPath) } +bool +Preferences::EntryOpenAsHtml() +{ + return fOpenAs == kOpenAsHtml; +} + + +bool +Preferences::SetEntryOpenAsHtml(bool asHtml) +{ + if (asHtml == true) + fOpenAs = kOpenAsHtml; + else + fOpenAs = kOpenAsUrl; + return asHtml; +} + + diff --git a/src/Preferences.h b/src/Preferences.h index 6264378..dbd160a 100644 --- a/src/Preferences.h +++ b/src/Preferences.h @@ -14,7 +14,6 @@ static const int64 HOUR_IN_MICROSECONDS = 3600000000; enum { - kOpenAsAuto = 0, kOpenAsHtml = 1, kOpenAsUrl = 2 }; @@ -38,6 +37,9 @@ public: BString EntryOpenWith(); status_t SetEntryOpenWith(const char* binPath); + bool EntryOpenAsHtml(); + bool SetEntryOpenAsHtml(bool asHtml); + bool fNewNotify; bool fFailureNotify; int8 fOpenAs; diff --git a/src/Util.cpp b/src/Util.cpp index a137da7..bf94b63 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -5,6 +5,8 @@ #include "Util.h" +#include +#include #include #include #include @@ -111,7 +113,18 @@ urlToFilename(BUrl url) return filename; } - + + +const char* +tempFileName(const char* dir, const char* name, const char* suffix) +{ + BString tmpDir("/tmp/"); + tmpDir << dir; + BDirectory().CreateDirectory(tmpDir.String(), NULL); + + tmpDir << "/" << name << "-" << (rand() % 1000) << "." << suffix; + return tmpDir.String(); +} int32 @@ -146,3 +159,34 @@ userFileError(status_t status, const char* path) } +entry_ref +tempHtmlFile(entry_ref* ref, const char* title) +{ + entry_ref tempRef; + BEntry(tempFileName("Pogger", title, "html")).GetRef(&tempRef); + + BFile ogFile(ref, B_READ_ONLY); + BFile htmlFile(&tempRef, B_WRITE_ONLY | B_CREATE_FILE); + + char readBuf[100] = {'\0'}; + ssize_t readLen = 1; + + BString head("\n\n"); + head << title << "\n\n\n"; + BString tail("\n\n"); + const void* headBuf = head.String(); + const void* tailBuf = tail.String(); + + htmlFile.Write(headBuf, head.Length()); + + while (readLen > 0) { + readLen = ogFile.Read(readBuf, 100); + htmlFile.Write(readBuf, readLen); + } + + htmlFile.Write(tailBuf, tail.Length()); + + return tempRef; +} + + diff --git a/src/Util.h b/src/Util.h index b2d6347..e135f03 100644 --- a/src/Util.h +++ b/src/Util.h @@ -24,11 +24,16 @@ bool withinDateRange(BDateTime, BDateTime, BDateTime); bool isRemotePath(BString); BString urlToFilename(BUrl url); +const char* tempFileName(const char* dir, const char* name, const char* suffix); int32 fetch(BUrl url, BDataIO* reply, BString* hash, int timeout); void userFileError(status_t status, const char* path); +// Takes a (probably invalid) bit of HTML and adds necessary elements in a +// temporary copy. Returns an entry_ref to said temporary copy. +entry_ref tempHtmlFile(entry_ref* ref, const char* title); + #endif // UTIL_H