1/*****************************************************************************/
2// Print to file transport add-on.
3//
4// Author
5//   Philippe Houdoin
6//
7// This application and all source files used in its construction, except
8// where noted, are licensed under the MIT License, and have been written
9// and are:
10//
11// Copyright (c) 2001,2002 Haiku Project
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30/*****************************************************************************/
31
32#include <stdio.h>
33
34#include <StorageKit.h>
35#include <SupportKit.h>
36
37#include "FileSelector.h"
38
39static BList *	gFiles 	= NULL;
40
41status_t	AddFile(BFile * file);
42status_t	RemoveFile();
43
44static const int32 kStatusOk = 'okok';
45static const int32 kStatusCancel = 'canc';
46
47extern "C" _EXPORT void exit_transport()
48{
49	RemoveFile();
50}
51
52extern "C" _EXPORT BDataIO * init_transport(BMessage *	msg)
53{
54	FileSelector * selector = new FileSelector();
55	entry_ref ref;
56	if (selector->Go(&ref) != B_OK) {
57		// dialog cancelled
58		if (msg)
59			msg->what = kStatusCancel;
60
61		// for backwards compatibility with BeOS R5 return an invalid BFile
62		BFile *file = new BFile();
63		AddFile(file);
64		return file;
65	}
66
67	BFile *file = new BFile(&ref, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
68	if ( file->InitCheck() != B_OK ) {
69		// invalid file selected
70		if (msg)
71			msg->what = kStatusCancel;
72		AddFile(file);
73		return file;
74	}
75
76	if (msg) {
77		// Print transport add-ons should set to 'okok' the message on success
78		msg->what = kStatusOk;
79
80		BPath path;
81		path.SetTo(&ref);
82		msg->AddString("path", path.Path());
83	}
84	AddFile(file);
85	return file;
86}
87
88status_t AddFile(BFile * file)
89{
90	if (file == NULL)
91		return B_ERROR;
92
93	if (gFiles == NULL)
94		gFiles = new BList();
95
96	gFiles->AddItem(file);
97
98	return B_OK;
99}
100
101status_t RemoveFile()
102{
103	if (gFiles == NULL)
104		return B_OK;
105
106	int32 n = gFiles->CountItems();
107	for (int32 i = 0; i < n; i++)
108		delete (BFile*)gFiles->ItemAt(i);
109
110	delete gFiles;
111	gFiles = NULL;
112	return B_OK;
113}
114