1// ContainerApplication.cpp
2
3
4#include "ContainerApplication.h"
5
6
7const char *XSHELF_INSPECTOR_APP	= "application/x-vnd.reh-XShelfInspector";	// Mime XShelfInspector
8const char *BACKSTORE_PATH				= "XContainer/XContainer_data";
9
10
11ContainerApplication :: ContainerApplication()
12		  		  					: IEApplication("application/x-vnd.reh-XContainer")
13{
14	fTest 	= false;
15	fStream = NULL;
16	fRemove = false;
17	BEntry		*entry;
18	fContainerWindow = NULL;
19
20	if ((entry = archive_file()))
21	{
22		fStream = new BFile(entry, O_RDWR);
23		delete entry;
24	}
25
26	fContainerWindow = new ContainerWindow(fStream);
27}
28
29
30ContainerApplication :: ~ ContainerApplication()
31{
32	be_roster -> StopWatching(fMsgnr);
33	if (fStream != NULL) delete fStream;
34
35	if ( (fRemove == true) && (fTest == false)	)	// delete backing store file
36	{
37		BPath		path;
38		BPath		parent;
39		if (find_directory (B_USER_SETTINGS_DIRECTORY, &path, true) != B_OK) return; // path: /boot/home/config/settings
40		path.Append (BACKSTORE_PATH);
41
42		path.GetParent(&parent);										// lock for parent
43		create_directory(parent.Path(), 0777);			// create directory
44		parent.Unset();															// close
45
46		BEntry	entry(path.Path());
47		entry_ref	ref;
48		entry.GetRef(&ref);
49
50		BFile file;
51		file.SetTo(&ref,B_ERASE_FILE);							// erase file
52		file.Unset();
53	}
54}
55
56
57
58BEntry  *ContainerApplication :: archive_file(bool create)
59{
60	BPath		path;
61	BEntry	*entry = NULL;
62	BPath		parent;
63
64	if (find_directory (B_USER_SETTINGS_DIRECTORY, &path, true) != B_OK)	// path: /boot/home/config/settings
65		return NULL;
66
67	path.Append (BACKSTORE_PATH);
68
69	if (create)
70	{
71		path.GetParent(&parent);											// lock for parent
72		create_directory(parent.Path(), 0777);				// create directory
73		parent.Unset();																// close
74
75		int	fd;
76		fd  = open(path.Path(), O_RDWR);
77		if (fd < 0)  fd = creat(path.Path(), 0777);
78		if (fd > 0)  close(fd);
79	}
80
81	entry = new BEntry(path.Path());
82	if (entry->InitCheck() != B_NO_ERROR)
83	{
84		delete entry;
85		entry = NULL;
86	}
87	return entry;
88}
89
90
91void ContainerApplication::MessageReceived(BMessage *message)
92{
93
94	switch(message->what)
95	{
96		case CLEAR_FILE:
97			{
98				message -> FindBool("Remove",&fRemove);
99			}
100			break;
101
102		case TEST_REPLICANT:
103			{
104				message -> FindBool("Test",&fTest);
105			}
106			break;
107
108		case B_SOME_APP_ACTIVATED:
109		case B_SOME_APP_LAUNCHED:
110			{
111				fContainerWindow -> Lock();
112					bool value =  be_roster -> IsRunning(XSHELF_INSPECTOR_APP);
113					fContainerWindow->EnableRemoveButton(value);									// Only when XShelf.. is running
114					fContainerWindow->EnableTestButton(value);										// Buttons are enabled
115				fContainerWindow -> Unlock();
116			}
117			break;
118
119		case B_SOME_APP_QUIT:															// Quit XContaier when XShelfInspector is quitting
120			{
121				fTest = false;
122				bool value =  be_roster -> IsRunning(XSHELF_INSPECTOR_APP);
123				if ( !value && fContainerWindow->Lock() ) fContainerWindow -> QuitRequested();
124			}
125			break;
126
127		default:
128			BApplication::MessageReceived(message);
129			break;
130	}
131
132}
133
134void ContainerApplication::ReadyToRun()
135{
136	fMsgnr = BMessenger(this);
137	be_roster -> StartWatching(fMsgnr);
138	PostMessage(B_SOME_APP_LAUNCHED);
139}
140
141
142
143int main()
144{
145	ContainerApplication	theApplication;
146
147	theApplication.Run();
148
149	return(0);
150}
151