Make StyledEdit check its open windows before launching a new one for an entry_ref. Simplify the application class somewhat by removing DispatchMessage() and replacing the custom made ArgvReceivedEx() with a standard ArgvReceived(). Rely on BPath to take care of paths relative to the current working directory - don't do it manually.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30701 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jonas Sundström 2009-05-10 21:33:23 +00:00
parent 7f33d2c159
commit 13e9a630b9
4 changed files with 98 additions and 54 deletions

View File

@ -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<StyledEditWindow*>(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();
}

View File

@ -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;

View File

@ -6,6 +6,7 @@
* Mattias Sundblad
* Andrew Bachmann
* Philippe Saint-Pierre
* Jonas Sundström
*/
#include "Constants.h"
@ -27,6 +28,7 @@
#include <Menu.h>
#include <MenuBar.h>
#include <MenuItem.h>
#include <Path.h>
#include <PrintJob.h>
#include <Rect.h>
#include <Roster.h>
@ -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;
}

View File

@ -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);