xref: /haiku/src/kits/package/Context.cpp (revision a085e81e62d7a860f809b4fb7c7bf5654c396985)
1 /*
2  * Copyright 2011, 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 BContext::BContext(BDecisionProvider& decisionProvider,
33 	BJobStateListener& jobStateListener)
34 	:
35 	fDecisionProvider(decisionProvider),
36 	fJobStateListener(jobStateListener),
37 	fTempfileManager(NULL)
38 {
39 	fInitStatus = _Initialize();
40 }
41 
42 
43 BContext::~BContext()
44 {
45 	delete fTempfileManager;
46 }
47 
48 
49 status_t
50 BContext::InitCheck() const
51 {
52 	return fInitStatus;
53 }
54 
55 
56 status_t
57 BContext::GetNewTempfile(const BString& baseName, BEntry* entry) const
58 {
59 	if (entry == NULL)
60 		return B_BAD_VALUE;
61 	if (fTempfileManager == NULL)
62 		return B_NO_INIT;
63 	*entry = fTempfileManager->Create(baseName);
64 	return entry->InitCheck();
65 }
66 
67 
68 BJobStateListener&
69 BContext::JobStateListener() const
70 {
71 	return fJobStateListener;
72 }
73 
74 
75 BDecisionProvider&
76 BContext::DecisionProvider() const
77 {
78 	return fDecisionProvider;
79 }
80 
81 
82 status_t
83 BContext::_Initialize()
84 {
85 	fTempfileManager = new (std::nothrow) TempfileManager();
86 	if (fTempfileManager == NULL)
87 		return B_NO_MEMORY;
88 
89 	BPath tempPath;
90 	status_t result = find_directory(B_COMMON_TEMP_DIRECTORY, &tempPath, true);
91 	if (result != B_OK)
92 		return result;
93 	BDirectory tempDirectory(tempPath.Path());
94 	if ((result = tempDirectory.InitCheck()) != B_OK)
95 		return result;
96 
97 	BString contextName = BString("pkgkit-context-") << find_thread(NULL);
98 	BDirectory baseDirectory;
99 	result = tempDirectory.CreateDirectory(contextName.String(),
100 		&baseDirectory);
101 	if (result != B_OK)
102 		return result;
103 
104 	fTempfileManager->SetBaseDirectory(baseDirectory);
105 
106 	return B_OK;
107 }
108 
109 
110 }	// namespace BPackageKit
111