xref: /haiku/src/apps/diskusage/App.cpp (revision 71452e98334eaac603bf542d159e24788a46bebb)
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 
25 App::App()
26 	:
27 	BApplication(kAppSignature),
28 	fMainWindow(NULL), fSavedRefsReceived(NULL)
29 {
30 	// Get a reference to the help file.
31 	BPath path;
32 	if (find_directory(B_BEOS_DOCUMENTATION_DIRECTORY, &path) == B_OK
33 		&& path.Append(kHelpFileName) == B_OK) {
34 		printf("help file =? %s\n", path.Path());
35 		BEntry entry(path.Path());
36 		helpFileWasFound = entry.Exists();
37 		entry.GetRef(&helpFileRef);
38 	} else
39 		helpFileWasFound = false;
40 }
41 
42 
43 App::~App()
44 {
45 	delete fSavedRefsReceived;
46 }
47 
48 
49 void
50 App::ArgvReceived(int32 argc, char** argv)
51 {
52 	BMessage refsReceived(B_REFS_RECEIVED);
53 	for (int32 i = 1; i < argc; i++) {
54 		BEntry entry(argv[i], true);
55 		entry_ref ref;
56 		if (entry.GetRef(&ref) == B_OK)
57 			refsReceived.AddRef("refs", &ref);
58 	}
59 	if (refsReceived.HasRef("refs"))
60 		PostMessage(&refsReceived);
61 }
62 
63 
64 void
65 App::RefsReceived(BMessage* message)
66 {
67 	if (!message->HasRef("refs") && message->HasRef("dir_ref")) {
68 		entry_ref dirRef;
69 		if (message->FindRef("dir_ref", &dirRef) == B_OK)
70 			message->AddRef("refs", &dirRef);
71 	}
72 
73 	if (fMainWindow == NULL) {
74 		// ReadyToRun() has not been called yet, this happens when someone
75 		// launches us with a B_REFS_RECEIVED message.
76 		delete fSavedRefsReceived;
77 		fSavedRefsReceived = new BMessage(*message);
78 	} else
79 		fMainWindow->PostMessage(message);
80 }
81 
82 
83 void
84 App::ReadyToRun()
85 {
86 	BRect frame;
87 
88 	BPath path;
89 	BFile settingsFile;
90 	BMessage settings;
91 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK
92 		|| path.Append("DiskUsage") != B_OK
93 		|| settingsFile.SetTo(path.Path(), B_READ_ONLY) != B_OK
94 		|| settings.Unflatten(&settingsFile) != B_OK
95 		|| settings.FindRect("window frame", &frame) != B_OK) {
96 		// use default window frame
97 		frame.Set(0, 0, kDefaultPieSize, kDefaultPieSize);
98 		frame.OffsetTo(50, 50);
99 	}
100 
101 	fMainWindow = new MainWindow(frame);
102 	fMainWindow->Show();
103 
104 	if (fSavedRefsReceived) {
105 		// RefsReceived() was called earlier than ReadyToRun()
106 		fMainWindow->PostMessage(fSavedRefsReceived);
107 		delete fSavedRefsReceived;
108 		fSavedRefsReceived = NULL;
109 	}
110 }
111 
112 
113 bool
114 App::QuitRequested()
115 {
116 	// Save the settings.
117 	BPath path;
118 	BFile settingsFile;
119 	BMessage settings;
120 	if (settings.AddRect("window frame", fMainWindow->Frame()) != B_OK
121 		|| find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK
122 		|| path.Append("DiskUsage") != B_OK
123 		|| settingsFile.SetTo(path.Path(),
124 			B_CREATE_FILE | B_WRITE_ONLY | B_ERASE_FILE) != B_OK
125 		|| settings.Flatten(&settingsFile) != B_OK) {
126 		fprintf(stderr, "Failed to write application settings.\n");
127 	}
128 
129 	return BApplication::QuitRequested();
130 }
131