xref: /haiku/src/apps/diskusage/App.cpp (revision 6f80a9801fedbe7355c4360bd204ba746ec3ec2d)
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),
29 	fSavedRefsReceived(NULL)
30 {
31 }
32 
33 
34 App::~App()
35 {
36 	delete fSavedRefsReceived;
37 }
38 
39 
40 void
41 App::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 
55 void
56 App::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 
74 void
75 App::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->MoveOnScreen();
94 	fMainWindow->Show();
95 
96 	if (fSavedRefsReceived) {
97 		// RefsReceived() was called earlier than ReadyToRun()
98 		fMainWindow->PostMessage(fSavedRefsReceived);
99 		delete fSavedRefsReceived;
100 		fSavedRefsReceived = NULL;
101 	}
102 }
103 
104 
105 bool
106 App::QuitRequested()
107 {
108 	// Save the settings.
109 	BPath path;
110 	BFile settingsFile;
111 	BMessage settings;
112 	if (settings.AddRect("window frame", fMainWindow->Frame()) != B_OK
113 		|| find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK
114 		|| path.Append("DiskUsage") != B_OK
115 		|| settingsFile.SetTo(path.Path(),
116 			B_CREATE_FILE | B_WRITE_ONLY | B_ERASE_FILE) != B_OK
117 		|| settings.Flatten(&settingsFile) != B_OK) {
118 		fprintf(stderr, "Failed to write application settings.\n");
119 	}
120 
121 	return BApplication::QuitRequested();
122 }
123