11556Srgrimes/*****************************************************************************/
21556Srgrimes// Print to file transport add-on.
31556Srgrimes//
41556Srgrimes// Author
51556Srgrimes//   Philippe Houdoin
61556Srgrimes//
71556Srgrimes// This application and all source files used in its construction, except
81556Srgrimes// where noted, are licensed under the MIT License, and have been written
91556Srgrimes// and are:
101556Srgrimes//
111556Srgrimes// Copyright (c) 2001,2002 OpenBeOS Project
121556Srgrimes//
131556Srgrimes// Permission is hereby granted, free of charge, to any person obtaining a
141556Srgrimes// copy of this software and associated documentation files (the "Software"),
151556Srgrimes// to deal in the Software without restriction, including without limitation
161556Srgrimes// the rights to use, copy, modify, merge, publish, distribute, sublicense,
171556Srgrimes// and/or sell copies of the Software, and to permit persons to whom the
181556Srgrimes// Software is furnished to do so, subject to the following conditions:
191556Srgrimes//
201556Srgrimes// The above copyright notice and this permission notice shall be included
211556Srgrimes// in all copies or substantial portions of the Software.
221556Srgrimes//
231556Srgrimes// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
241556Srgrimes// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
251556Srgrimes// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
261556Srgrimes// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
271556Srgrimes// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
281556Srgrimes// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
291556Srgrimes// DEALINGS IN THE SOFTWARE.
301556Srgrimes/*****************************************************************************/
311556Srgrimes
321556Srgrimes#include <stdio.h>
331556Srgrimes
341556Srgrimes#include <StorageKit.h>
351556Srgrimes#include <SupportKit.h>
361556Srgrimes
371556Srgrimes#include "FileSelector.h"
3820412Ssteve
391556Srgrimesstatic BList *	gFiles 	= NULL;
401556Srgrimes
411556Srgrimesstatus_t	AddFile(BFile * file);
421556Srgrimesstatus_t	RemoveFile();
431556Srgrimes
4435772Scharnierstatic const int32 kStatusOk = 'okok';
4536000Scharnierstatic const int32 kStatusCancel = 'canc';
4635772Scharnier
4735772Scharnierextern "C" _EXPORT void exit_transport()
4850471Speter{
491556Srgrimes	RemoveFile();
501556Srgrimes}
511556Srgrimes
521556Srgrimesextern "C" _EXPORT BDataIO * init_transport(BMessage *	msg)
531556Srgrimes{
541556Srgrimes	FileSelector * selector = new FileSelector();
551556Srgrimes	entry_ref ref;
561556Srgrimes	if (selector->Go(&ref) != B_OK) {
5718578Sache		// dialog cancelled
581556Srgrimes		if (msg)
591556Srgrimes			msg->what = kStatusCancel;
601556Srgrimes
611556Srgrimes		// for backwards compatibility with BeOS R5 return an invalid BFile
621556Srgrimes		BFile *file = new BFile();
631556Srgrimes		AddFile(file);
6439065Simp		return file;
651556Srgrimes	}
661556Srgrimes
671556Srgrimes	BFile *file = new BFile(&ref, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
6839138Simp	if ( file->InitCheck() != B_OK ) {
691556Srgrimes		// invalid file selected
701556Srgrimes		if (msg)
711556Srgrimes			msg->what = kStatusCancel;
721556Srgrimes		AddFile(file);
7339138Simp		return file;
7439138Simp	}
7539138Simp
761556Srgrimes	if (msg) {
771556Srgrimes		// Print transport add-ons should set to 'okok' the message on success
781556Srgrimes		msg->what = kStatusOk;
7918578Sache
8018578Sache		BPath path;
8124348Simp		path.SetTo(&ref);
821556Srgrimes		msg->AddString("path", path.Path());
831556Srgrimes	}
841556Srgrimes	AddFile(file);
851556Srgrimes	return file;
861556Srgrimes}
871556Srgrimes
881556Srgrimesstatus_t AddFile(BFile * file)
891556Srgrimes{
901556Srgrimes	if (file == NULL)
911556Srgrimes		return B_ERROR;
921556Srgrimes
931556Srgrimes	if (gFiles == NULL)
941556Srgrimes		gFiles = new BList();
951556Srgrimes
961556Srgrimes	gFiles->AddItem(file);
971556Srgrimes
981556Srgrimes	return B_OK;
9959239Sasmodai}
1001556Srgrimes
1011556Srgrimesstatus_t RemoveFile()
1021556Srgrimes{
1031556Srgrimes	if (gFiles == NULL)
10418546Simp		return B_OK;
1051556Srgrimes
1061556Srgrimes	int32 n = gFiles->CountItems();
1071556Srgrimes	for (int32 i = 0; i < n; i++)
1081556Srgrimes		delete (BFile*)gFiles->ItemAt(i);
1091556Srgrimes
1101556Srgrimes	delete gFiles;
1111556Srgrimes	gFiles = NULL;
1121556Srgrimes	return B_OK;
1131556Srgrimes}
1141556Srgrimes