rearch
[tlb/tlb-podcasts.git] / tlb / core / src / AppManager.cpp
1 #include <AppManager.h>
2
3 #include <stdio.h>
4 #include <QFile>
5
6 static AppManager *s_am = nullptr;
7 static Shows *s_shows = nullptr;
8
9 AppManager::AppManager(QObject* parent) :
10 QObject(parent)
11 {
12 s_am = this;
13 s_shows = new Shows(dataDirectory(), this);
14
15 connect(this, &AppManager::internalNewSource,
16 this, [this](QUrl sourceFile){
17 m_view->setSource(sourceFile);
18 currentPage = sourceFile;
19 }, Qt::QueuedConnection);
20 }
21
22 AppManager::~AppManager()
23 {
24
25 }
26
27 QString AppManager::dataDirectory(void) const
28 {
29 return QStringLiteral("/home/tlb/dev/git/android-qt-sandbox/data/");
30 }
31
32 void AppManager::initializeView(QUrl url)
33 {
34 m_view = new QQuickView();
35 m_view->setFlags(Qt::Window);
36 m_view->setResizeMode(QQuickView::SizeRootObjectToView);
37 m_view->resize({450, 800});
38 m_view->show();
39 m_view->setSource(url);
40
41 // this url is the base of the stack.
42 currentPage = url;
43 }
44
45 AppManager *AppManager::create(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
46 {
47 QJSEngine::setObjectOwnership(s_am, QJSEngine::CppOwnership);
48 return s_am;
49 }
50
51 QObject *AppManager::shows(void)
52 {
53 return s_shows;
54 }
55
56 void AppManager::requestBack()
57 {
58 fprintf(stderr, "[AM] received request go back\n");
59 if(m_pageHistory.size() > 0){
60 // we have something to go back to.
61 fprintf(stderr, "[AM] -- going back to %s\n",
62 m_pageHistory.top().toString().toStdString().c_str());
63 emit internalNewSource(m_pageHistory.pop());
64 }else{
65 fprintf(stderr, "[AM] --error, no where to run to\n");
66 }
67 }
68
69 void AppManager::requestNewSource(QUrl sourceFile)
70 {
71 fprintf(stderr, "[AM] received request to load source file '%s'\n",
72 sourceFile.toString().toStdString().c_str());
73 if(QFile::exists(sourceFile.toString().remove("qrc"))){
74 // when we go someplace new, push what we have now into the stack.
75 m_pageHistory.push(currentPage);
76 currentPage = sourceFile;
77 emit internalNewSource(sourceFile);
78 }else{
79 fprintf(stderr, "[AM] --error! no such file\n");
80 }
81 }