diff --git a/src/apps/stylededit/StyledEditApp.cpp b/src/apps/stylededit/StyledEditApp.cpp index 23605e38fa..eb4e9177ad 100644 --- a/src/apps/stylededit/StyledEditApp.cpp +++ b/src/apps/stylededit/StyledEditApp.cpp @@ -5,6 +5,7 @@ * Authors: * Mattias Sundblad * Andrew Bachmann + * Jonas Sundström */ @@ -115,6 +116,7 @@ StyledEditApp::StyledEditApp() fWindowCount = 0; fNextUntitledWindow = 1; + fBadArguments = false; styled_edit_app = this; } @@ -126,31 +128,6 @@ StyledEditApp::~StyledEditApp() } -void -StyledEditApp::DispatchMessage(BMessage *msg, BHandler *handler) -{ - if (msg->what == B_ARGV_RECEIVED) { - int32 argc; - if (msg->FindInt32("argc", &argc) != B_OK) - argc = 0; - - const char** argv = new const char*[argc]; - for (int arg = 0; arg < argc; arg++) { - if (msg->FindString("argv", arg, &argv[arg]) != B_OK) { - argv[arg] = ""; - } - } - const char* cwd; - if (msg->FindString("cwd", &cwd) != B_OK) - cwd = ""; - - ArgvReceivedEx(argc, argv, cwd); - delete[] argv; - } else - BApplication::DispatchMessage(msg, handler); -} - - void StyledEditApp::MessageReceived(BMessage *message) { @@ -188,12 +165,56 @@ StyledEditApp::OpenDocument() } -void +status_t StyledEditApp::OpenDocument(entry_ref* ref) { + // traverse eventual symlink + BEntry entry(ref, true); + entry.GetRef(ref); + + if (entry.IsDirectory()) { + BPath path(&entry); + fprintf(stderr, + "Can't open directory \"%s\" for editing.\n", + path.Path()); + return B_ERROR; + } + + BEntry parent; + entry.GetParent(&parent); + + if (!entry.Exists() && !parent.Exists()) { + fprintf(stderr, + "Can't create file. Missing parent directory.\n"); + return B_ERROR; + } + + BWindow* window = NULL; + StyledEditWindow* document = NULL; + + for (int32 index = 0; ; index++) { + window = WindowAt(index); + if (window == NULL) + break; + + document = dynamic_cast(window); + if (document == NULL) + continue; + + if (document->IsDocumentEntryRef(ref)) { + if (document->Lock()) { + document->Activate(); + document->Unlock(); + return B_OK; + } + } + } + cascade(); new StyledEditWindow(gWindowRect, ref, fOpenAsEncoding); fWindowCount++; + + return B_OK; } @@ -222,32 +243,18 @@ StyledEditApp::RefsReceived(BMessage *message) void -StyledEditApp::ArgvReceivedEx(int32 argc, const char* argv[], const char* cwd) +StyledEditApp::ArgvReceived(int32 argc, char* argv[]) { for (int i = 1 ; (i < argc) ; i++) { - BPath path; - if (argv[i][0] == '/') - path.SetTo(argv[i]); - else - path.SetTo(cwd, argv[i]); + BPath path(argv[i]); + entry_ref ref; + get_ref_for_path(path.Path(), &ref); + + status_t status; + status = OpenDocument(&ref); - if (path.InitCheck() != B_OK) { - fprintf(stderr, "Setting path failed: \""); - if (argv[i][0] == '/') - fprintf(stderr, "%s\".\n", argv[i]); - else - fprintf(stderr, "%s/%s\".\n", cwd, argv[i]); - continue; - } - - entry_ref ref; - status_t status = get_ref_for_path(path.Path(), &ref); - if (status != B_OK) { - fprintf(stderr, "Could not open \"%s\": %s.\n", path.Path(), strerror(status)); - continue; - } - - OpenDocument(&ref); + if (status != B_OK && IsLaunching()) + fBadArguments = true; } } @@ -255,7 +262,12 @@ StyledEditApp::ArgvReceivedEx(int32 argc, const char* argv[], const char* cwd) void StyledEditApp::ReadyToRun() { - if (fWindowCount == 0) + if (fWindowCount > 0) + return; + + if (fBadArguments) + Quit(); + else OpenDocument(); } diff --git a/src/apps/stylededit/StyledEditApp.h b/src/apps/stylededit/StyledEditApp.h index 8026c24e31..7008237302 100644 --- a/src/apps/stylededit/StyledEditApp.h +++ b/src/apps/stylededit/StyledEditApp.h @@ -5,6 +5,7 @@ * Authors: * Mattias Sundblad * Andrew Bachmann + * Jonas Sundström */ #ifndef STYLED_EDIT_APP #define STYLED_EDIT_APP @@ -28,14 +29,13 @@ class StyledEditApp : public BApplication { virtual ~StyledEditApp(); virtual void MessageReceived(BMessage *message); + virtual void ArgvReceived(int32 argc, char** argv); virtual void RefsReceived(BMessage *message); virtual void ReadyToRun(); - virtual void DispatchMessage(BMessage *an_event, BHandler *handler); - int32 NumberOfWindows(); void OpenDocument(); - void OpenDocument(entry_ref *ref); + status_t OpenDocument(entry_ref *ref); void CloseDocument(); private: @@ -47,7 +47,7 @@ class StyledEditApp : public BApplication { uint32 fOpenAsEncoding; int32 fWindowCount; int32 fNextUntitledWindow; - + bool fBadArguments; }; extern StyledEditApp* styled_edit_app; diff --git a/src/apps/stylededit/StyledEditWindow.cpp b/src/apps/stylededit/StyledEditWindow.cpp index e7475b15da..ed00091fe8 100644 --- a/src/apps/stylededit/StyledEditWindow.cpp +++ b/src/apps/stylededit/StyledEditWindow.cpp @@ -6,6 +6,7 @@ * Mattias Sundblad * Andrew Bachmann * Philippe Saint-Pierre + * Jonas Sundström */ #include "Constants.h" @@ -27,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -1345,3 +1347,31 @@ StyledEditWindow::_ShowAlert(const BString& text, const BString& label, return alert->Go(); } + + +bool +StyledEditWindow::IsDocumentEntryRef(const entry_ref* ref) +{ + if (ref == NULL) + return false; + + if (fSaveMessage == NULL) + return false; + + entry_ref dir; + const char* name; + if (fSaveMessage->FindRef("directory", &dir) != B_OK + || fSaveMessage->FindString("name", &name) != B_OK) + return false; + + entry_ref documentRef; + BPath documentPath(&dir); + documentPath.Append(name); + get_ref_for_path(documentPath.Path(), &documentRef); + + if (*ref == documentRef) + return true; + + return false; +} + diff --git a/src/apps/stylededit/StyledEditWindow.h b/src/apps/stylededit/StyledEditWindow.h index a7419ca2df..bdb3f72ac9 100644 --- a/src/apps/stylededit/StyledEditWindow.h +++ b/src/apps/stylededit/StyledEditWindow.h @@ -5,6 +5,7 @@ * Authors: * Mattias Sundblad * Andrew Bachmann + * Jonas Sundström */ #ifndef STYLED_EDIT_WINDOW_H #define STYLED_EDIT_WINDOW_H @@ -43,6 +44,7 @@ class StyledEditWindow : public BWindow { status_t PageSetup(const char *documentname); void Print(const char *documentname); void SearchAllWindows(BString find, BString replace, bool casesens); + bool IsDocumentEntryRef(const entry_ref *ref); private: void InitWindow(uint32 encoding = 0);