1/*
2 * Copyright (c) 2008 Stephan A��mus <superstippi@gmx.de>. All rights reserved.
3 * Distributed under the terms of the MIT/X11 license.
4 *
5 * Copyright (c) 1999 Mike Steed. You are free to use and distribute this
6 * software as long as it is accompanied by it's documentation and this
7 * copyright notice. The software comes with no warranty, etc.
8 */
9
10
11#include "App.h"
12
13#include <stdio.h>
14
15#include <Entry.h>
16#include <File.h>
17#include <FindDirectory.h>
18#include <Node.h>
19#include <Path.h>
20
21#include "DiskUsage.h"
22#include "MainWindow.h"
23
24
25App::App()
26	:
27	BApplication(kAppSignature),
28	fMainWindow(NULL),
29	fSavedRefsReceived(NULL)
30{
31}
32
33
34App::~App()
35{
36	delete fSavedRefsReceived;
37}
38
39
40void
41App::ArgvReceived(int32 argc, char** argv)
42{
43	BMessage refsReceived(B_REFS_RECEIVED);
44	for (int32 i = 1; i < argc; i++) {
45		BEntry entry(argv[i], true);
46		entry_ref ref;
47		if (entry.GetRef(&ref) == B_OK)
48			refsReceived.AddRef("refs", &ref);
49	}
50	if (refsReceived.HasRef("refs"))
51		PostMessage(&refsReceived);
52}
53
54
55void
56App::RefsReceived(BMessage* message)
57{
58	if (!message->HasRef("refs") && message->HasRef("dir_ref")) {
59		entry_ref dirRef;
60		if (message->FindRef("dir_ref", &dirRef) == B_OK)
61			message->AddRef("refs", &dirRef);
62	}
63
64	if (fMainWindow == NULL) {
65		// ReadyToRun() has not been called yet, this happens when someone
66		// launches us with a B_REFS_RECEIVED message.
67		delete fSavedRefsReceived;
68		fSavedRefsReceived = new BMessage(*message);
69	} else
70		fMainWindow->PostMessage(message);
71}
72
73
74void
75App::ReadyToRun()
76{
77	BRect frame;
78
79	BPath path;
80	BFile settingsFile;
81	BMessage settings;
82	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK
83		|| path.Append("DiskUsage") != B_OK
84		|| settingsFile.SetTo(path.Path(), B_READ_ONLY) != B_OK
85		|| settings.Unflatten(&settingsFile) != B_OK
86		|| settings.FindRect("window frame", &frame) != B_OK) {
87		// use default window frame
88		frame.Set(0, 0, kDefaultPieSize, kDefaultPieSize);
89		frame.OffsetTo(50, 50);
90	}
91
92	fMainWindow = new MainWindow(frame);
93	fMainWindow->Show();
94
95	if (fSavedRefsReceived) {
96		// RefsReceived() was called earlier than ReadyToRun()
97		fMainWindow->PostMessage(fSavedRefsReceived);
98		delete fSavedRefsReceived;
99		fSavedRefsReceived = NULL;
100	}
101}
102
103
104bool
105App::QuitRequested()
106{
107	// Save the settings.
108	BPath path;
109	BFile settingsFile;
110	BMessage settings;
111	if (settings.AddRect("window frame", fMainWindow->Frame()) != B_OK
112		|| find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK
113		|| path.Append("DiskUsage") != B_OK
114		|| settingsFile.SetTo(path.Path(),
115			B_CREATE_FILE | B_WRITE_ONLY | B_ERASE_FILE) != B_OK
116		|| settings.Flatten(&settingsFile) != B_OK) {
117		fprintf(stderr, "Failed to write application settings.\n");
118	}
119
120	return BApplication::QuitRequested();
121}
122