-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCardPresenter.cpp
More file actions
355 lines (284 loc) · 8.36 KB
/
Copy pathCardPresenter.cpp
File metadata and controls
355 lines (284 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "CardPresenter.h"
#include "AIReading.h"
#include "CardModel.h"
#include "CardView.h"
#include "Config.h"
#include "Reading.h"
#include <Directory.h>
#include <File.h>
#include <FindDirectory.h>
#include <Node.h>
#include <Path.h>
#include <View.h>
#include <chrono>
#include <ctime>
#include <iostream>
#include <thread>
CardPresenter::CardPresenter(CardModel* model, CardView* view)
:
fModel(model),
fView(view),
fReading(nullptr),
fCurrentReading(""),
fSpread(Config::GetSpread())
{
if (fModel)
fModel->Initialize();
if (fView)
fView->SetSpread(fSpread);
}
CardPresenter::~CardPresenter()
{
// Wait for any ongoing reading generation to complete
if (fReadingFuture.valid())
fReadingFuture.wait();
delete fReading;
delete fModel;
}
BView*
CardPresenter::GetView()
{
return fView;
}
void
CardPresenter::SetView(CardView* view)
{
fView = view;
if (fView)
fView->SetSpread(fSpread);
}
BString
CardPresenter::GetAPIKey()
{
return Config::GetAPIKey();
}
void
CardPresenter::SetAPIKey(const BString& apiKey)
{
Config::SetAPIKey(apiKey);
}
void
CardPresenter::SetSpread(const BString& spreadName)
{
SpreadType newSpread;
if (spreadName == "Three Card")
newSpread = THREE_CARD;
else if (spreadName == "Tree of Life")
newSpread = TREE_OF_LIFE;
else
return;
fSpread = newSpread;
Config::SetSpread(newSpread);
fView->SetSpread(newSpread);
}
void
CardPresenter::SetFontSize(float fontSize)
{
Config::SetFontSize(fontSize);
fView->SetFontSize(fontSize);
}
void
CardPresenter::NewReading()
{
fModel->ClearCurrentSpread();
if (fSpread == THREE_CARD)
LoadThreeCardSpread();
else if (fSpread == TREE_OF_LIFE)
LoadTreeOfLifeSpread();
}
void
CardPresenter::OnFrameResized()
{
fView->RefreshLayout();
}
void
CardPresenter::SaveFile(const BPath& path)
{
BFile file;
status_t status = file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if (status != B_OK) {
std::cout << "Error creating file: " << strerror(status) << std::endl;
return;
}
std::vector<CardInfo> cards;
fModel->GetCardSpread(cards,
fSpread == THREE_CARD ? Config::kThreeCardSpreadCount : Config::kTreeOfLifeSpreadCount);
BString content = "Tarot Reading:\n\n";
content << "Spread: " << (fSpread == THREE_CARD ? "Three Card" : "Tree of Life") << "\n\n";
for (size_t i = 0; i < cards.size(); ++i)
content << "Card " << (i + 1) << ": " << cards[i].displayName << "\n";
content << "\nAI Reading:\n" << GetCurrentReading() << "\n";
file.Write(content.String(), content.Length());
file.Unset();
// Register the file with our application's MIME type
Config::RegisterFileWithMime(path.Path(), "application/x-vnd.Ace-of-Wands");
}
void
CardPresenter::OpenFile(const BPath& path)
{
BFile file;
status_t status = file.SetTo(path.Path(), B_READ_ONLY);
if (status != B_OK) {
std::cout << "Error opening file: " << strerror(status) << std::endl;
return;
}
off_t size;
file.GetSize(&size);
char* buffer = new char[size + 1];
file.Read(buffer, size);
buffer[size] = '\0';
BString content(buffer);
delete[] buffer;
file.Unset();
std::vector<CardInfo> loadedCards;
BString aiReadingText;
BString spreadLine; // Moved declaration here
int32 spreadStart = content.FindFirst("Spread:");
if (spreadStart != B_ERROR) {
int32 spreadEnd = content.FindFirst("\n", spreadStart);
content.CopyInto(spreadLine, spreadStart, spreadEnd - spreadStart);
spreadLine.Remove(0, spreadLine.FindFirst(":") + 2);
spreadLine.Trim();
SetSpread(spreadLine);
} else {
// If "Spread:" is not found, assume it's a Three Card spread for backward compatibility
spreadLine = "Three Card";
SetSpread(spreadLine);
}
int32 cardStart = content.FindFirst("Card 1:");
if (cardStart != B_ERROR) {
int numCards = 0;
if (spreadLine == "Three Card")
numCards = Config::kThreeCardSpreadCount;
else if (spreadLine == "Tree of Life")
numCards = Config::kTreeOfLifeSpreadCount;
for (int i = 0; i < numCards; ++i) {
BString cardLine;
int32 lineEnd = content.FindFirst("\n", cardStart);
if (lineEnd != B_ERROR) {
content.CopyInto(cardLine, cardStart, lineEnd - cardStart);
cardLine.Remove(0, cardLine.FindFirst(":") + 2);
cardLine.Trim();
CardInfo info;
info.displayName = cardLine;
info.resourceID = fModel->GetResourceID(cardLine);
loadedCards.push_back(info);
cardStart = lineEnd + 1;
} else {
break;
}
}
}
int32 aiReadingStart = content.FindFirst("AI Reading:");
if (aiReadingStart != B_ERROR) {
aiReadingStart = content.FindFirst("\n", aiReadingStart) + 1;
aiReadingText = content.String() + aiReadingStart;
aiReadingText.Trim();
}
int32 expectedCardCount = 0;
if (spreadLine == "Three Card")
expectedCardCount = Config::kThreeCardSpreadCount;
else if (spreadLine == "Tree of Life")
expectedCardCount = Config::kTreeOfLifeSpreadCount;
if (loadedCards.size() == static_cast<size_t>(expectedCardCount)) {
fModel->SetCardSpread(loadedCards);
fView->DisplayCards(loadedCards);
fView->DisplayReading(aiReadingText);
} else {
std::cout << "Error: Could not parse cards from file. Expected " << expectedCardCount
<< " cards, found " << loadedCards.size() << "." << std::endl;
}
}
void
CardPresenter::LoadThreeCardSpread()
{
std::vector<CardInfo> cards;
fModel->GetCardSpread(cards, Config::kThreeCardSpreadCount);
fView->DisplayCards(cards);
fView->DisplayReading("Fetching reading...");
// Launch asynchronous task to get the reading
fReadingFuture = std::async(std::launch::async, [this, cards]() {
BString reading;
if (Config::GetAPIKey().IsEmpty()) {
std::vector<BString> cardNames;
for (const auto& card : cards)
cardNames.push_back(card.displayName);
fReading = new Reading(cardNames);
reading = fReading->GetInterpretation();
} else {
// Get an AI reading for the cards
reading = AIReading::GetReading(cards, fSpread);
}
fCurrentReading = reading;
// Log the reading if enabled
if (Config::GetLogReadings())
SaveReadingToFile(cards, reading);
// Update the UI with the reading in a thread-safe manner
fView->UpdateReading(reading);
});
}
void
CardPresenter::LoadTreeOfLifeSpread()
{
std::vector<CardInfo> cards;
fModel->GetCardSpread(cards, Config::kTreeOfLifeSpreadCount);
fView->DisplayCards(cards);
// Show loading message while fetching AI reading
fView->DisplayReading("Fetching reading...");
// Launch asynchronous task to get the reading
fReadingFuture = std::async(std::launch::async, [this, cards]() {
BString reading;
if (Config::GetAPIKey().IsEmpty()) {
std::vector<BString> cardNames;
for (const auto& card : cards)
cardNames.push_back(card.displayName);
fReading = new Reading(cardNames);
reading = fReading->GetInterpretation();
} else {
// Get an AI reading for the cards
reading = AIReading::GetReading(cards, fSpread);
}
fCurrentReading = reading;
// Log the reading if enabled
if (Config::GetLogReadings())
SaveReadingToFile(cards, reading);
// Update the UI with the reading in a thread-safe manner
fView->UpdateReading(reading);
});
}
void
CardPresenter::SaveReadingToFile(const std::vector<CardInfo>& cards, const BString& reading)
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return;
path.Append("AceOfWands/readings");
// Create the directory if it doesn't exist
BDirectory dir;
if (dir.CreateDirectory(path.Path(), &dir) != B_OK && dir.SetTo(path.Path()) != B_OK)
return;
// Generate a filename based on timestamp
time_t now = time(NULL);
struct tm* tm_now = localtime(&now);
char filename[256];
strftime(filename, sizeof(filename), "reading_%Y-%m-%d_%H-%M-%S.txt", tm_now);
path.Append(filename);
BFile file;
status_t status = file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if (status != B_OK)
return;
BString content = "Tarot Reading\n";
content << "Date: " << ctime(&now); // ctime includes newline
content << "Spread: " << (fSpread == THREE_CARD ? "Three Card" : "Tree of Life") << "\n\n";
for (size_t i = 0; i < cards.size(); ++i)
content << "Card " << (i + 1) << ": " << cards[i].displayName << "\n";
content << "\nAI Reading:\n" << reading << "\n";
file.Write(content.String(), content.Length());
file.Unset();
// Register the file with MIME type
BNode node(path.Path());
if (node.InitCheck() == B_OK) {
const char* mimeType = "application/x-vnd.Ace-of-Wands";
node.WriteAttr("BEOS:TYPE", B_STRING_TYPE, 0, mimeType, strlen(mimeType) + 1);
}
}