xref: /haiku/src/kits/package/Context.cpp (revision d17092ceb18bf47a96dbaf8a1acf10e6e3070704)
1 /*
2  * Copyright 2011-2015, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Oliver Tappe <zooey@hirschkaefer.de>
7  */
8 
9 
10 #include <new>
11 
12 #include <package/Context.h>
13 #include <package/TempfileManager.h>
14 
15 #include <Directory.h>
16 #include <FindDirectory.h>
17 #include <OS.h>
18 #include <Path.h>
19 
20 
21 namespace BPackageKit {
22 
23 
24 using BPrivate::TempfileManager;
25 
26 
27 BDecisionProvider::~BDecisionProvider()
28 {
29 }
30 
31 
32 bool
33 BDecisionProvider::YesNoDecisionNeeded(const BString& description,
34 	const BString& question, const BString& yes, const BString& no,
35 	const BString& defaultChoice)
36 {
37 	return defaultChoice == yes;
38 }
39 
40 
41 BContext::BContext(BDecisionProvider& decisionProvider,
42 	BSupportKit::BJobStateListener& jobStateListener)
43 	:
44 	fDecisionProvider(decisionProvider),
45 	fJobStateListener(jobStateListener),
46 	fTempfileManager(NULL)
47 {
48 	fInitStatus = _Initialize();
49 }
50 
51 
52 BContext::~BContext()
53 {
54 	delete fTempfileManager;
55 }
56 
57 
58 status_t
59 BContext::InitCheck() const
60 {
61 	return fInitStatus;
62 }
63 
64 
65 status_t
66 BContext::GetNewTempfile(const BString& baseName, BEntry* entry) const
67 {
68 	if (entry == NULL)
69 		return B_BAD_VALUE;
70 	if (fTempfileManager == NULL)
71 		return B_NO_INIT;
72 	*entry = fTempfileManager->Create(baseName);
73 	return entry->InitCheck();
74 }
75 
76 
77 BSupportKit::BJobStateListener&
78 BContext::JobStateListener() const
79 {
80 	return fJobStateListener;
81 }
82 
83 
84 BDecisionProvider&
85 BContext::DecisionProvider() const
86 {
87 	return fDecisionProvider;
88 }
89 
90 
91 status_t
92 BContext::_Initialize()
93 {
94 	fTempfileManager = new (std::nothrow) TempfileManager();
95 	if (fTempfileManager == NULL)
96 		return B_NO_MEMORY;
97 
98 	BPath tempPath;
99 	status_t result = find_directory(B_SYSTEM_TEMP_DIRECTORY, &tempPath, true);
100 	if (result != B_OK)
101 		return result;
102 	BDirectory tempDirectory(tempPath.Path());
103 	if ((result = tempDirectory.InitCheck()) != B_OK)
104 		return result;
105 
106 	BString contextName = BString("pkgkit-context-") << find_thread(NULL)
107 		<< "-" << system_time();
108 	BDirectory baseDirectory;
109 	result = tempDirectory.CreateDirectory(contextName.String(),
110 		&baseDirectory);
111 	if (result != B_OK)
112 		return result;
113 
114 	fTempfileManager->SetBaseDirectory(baseDirectory);
115 
116 	return B_OK;
117 }
118 
119 
120 }	// namespace BPackageKit
121