summaryrefslogtreecommitdiff
path: root/plexOsd.cpp
blob: 8a54cc8e449a13ecffd7eb35b27c9d5111b1283f (plain)
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
#include "plexOsd.h"
#include "plex.h"

//////////////////////////////////////////////////////////////////////////////
//	cOsdMenu
//////////////////////////////////////////////////////////////////////////////
static std::shared_ptr<plexclient::Plexservice> pPlexService;

std::shared_ptr<plexclient::Plexservice> cPlexBrowser::pLastService;
int cPlexBrowser::lastCurrentItem;

cPlexBrowser::cPlexBrowser(const char *title, std::shared_ptr<plexclient::Plexservice> Service) :cOsdMenu(title)
{
	dsyslog("[plex]%s:\n", __FUNCTION__);
	pService = Service;
	pService->Authenticate();
	if(pService == pLastService) {
		pCont = pService->GetLastSection(true);
	} else {
		pCont = pService->GetSection(pService->StartUri);
	}
	SetMenuCategory(mcRecording);
	CreateMenu();
}

cPlexBrowser* cPlexBrowser::RecoverLastState()
{
	if (cPlexBrowser::pLastService != NULL) {
		cPlexBrowser* pBrowser = new cPlexBrowser("", cPlexBrowser::pLastService);
		return pBrowser;
	}
	return NULL;
}

void cPlexBrowser::CreateMenu()
{
	// Clear Menu
	Clear();
	// Directory or Video?
	if(pCont && pCont->m_vDirectories.size() > 0) {

		for(std::vector<plexclient::Directory>::iterator it = pCont->m_vDirectories.begin(); it != pCont->m_vDirectories.end(); ++it) {
			plexclient::Directory *pDir = &(*it);
			Add(new cPlexOsdItem( tr(pDir->GetTitle().c_str()), pDir) );
		}
	}

	if(pCont && pCont->m_vVideos.size() > 0) {
		for(std::vector<plexclient::Video>::iterator it = pCont->m_vVideos.begin(); it != pCont->m_vVideos.end(); ++it) {
			plexclient::Video *vid = &(*it); // cast raw pointer
			Add(new cPlexOsdItem( vid->GetTitle().c_str(), vid) );
		}
	}

	if(Count() < 1) {
		Add(new cPlexOsdItem("Empty"));
	} else if (pService == pLastService) {
		// recover last selected item
		cOsdItem* item = Get(lastCurrentItem);
		SetCurrent(item);
		pLastService = NULL;
	}

	Display();
}

eOSState cPlexBrowser::ProcessKey(eKeys key)
{
	eOSState state;

	// call standard function
	state = cOsdMenu::ProcessKey(key);

	int current = Current();		// get current menu item index
	cPlexOsdItem *item = static_cast<cPlexOsdItem*>(Get(current));

	if(item->IsVideo()) {
		if(item->GetAttachedVideo()->m_iViewCount > 0) SetHelp(tr("Info"), tr("Unscrobble"));
		else SetHelp(tr("Info"), tr("Scrobble"));
	} else {
		SetHelp(NULL);
	}

	switch (state) {
	case osUnknown:
		switch (key) {
		case kOk:
			return ProcessSelected();
		case kBack:
			return LevelUp();
		case kRed:
			std::cout << "RED";
			if(item->IsVideo()) {
				std::cout << " Video Info";
			}
			std::cout << std::endl;
			break;
		case kGreen:
			if(item->IsVideo()) {
				if(item->GetAttachedVideo()->m_iViewCount > 0) {
					if(item->GetAttachedVideo()->SetUnwatched()) {
						item->GetAttachedVideo()->UpdateFromServer();
					}
				} else {
					if(item->GetAttachedVideo()->SetWatched()) {
						item->GetAttachedVideo()->UpdateFromServer();
					}
				}
			}
			if(item->GetAttachedVideo()->m_iViewCount > 0) SetHelp(tr("Info"), tr("Unscrobble"));
			else SetHelp(tr("Info"), tr("Scrobble"));
			break;
		default:
			break;
		}
		break;
	case osBack:
		state = LevelUp();
		if (state == osEnd) {	// top level reached
			return osPlugin;
		}
	default:
		break;
	}
	return state;
}

eOSState cPlexBrowser::LevelUp()
{
	pCont = pService->GetLastSection();
	if(!pCont) {
		cPlexMenu::eShow = menuShow::MAIN;
		return osEnd;
	}
	cString title = cString::sprintf(tr("Browse Plex - %s"), tr(pCont->m_sTitle1.c_str()));
	SetTitle(title);
	CreateMenu();
	return osContinue;
}

eOSState cPlexBrowser::ProcessSelected()
{
	int current = Current();		// get current menu item index
	cPlexOsdItem *item = static_cast<cPlexOsdItem*>(Get(current));


	if(item->IsVideo()) {
		pLastService = pService;
		lastCurrentItem = current;
		cMyPlugin::PlayFile(*item->GetAttachedVideo());
		return osEnd;
	}

	if(item->IsDir()) {
		plexclient::Directory* pDir = item->GetAttachedDirectory();
		pCont = pService->GetSection(pDir->m_sKey);
		cString title = cString::sprintf(tr("Browse Plex - %s"), tr(pDir->m_sTitle.c_str()));
		SetTitle(title);
		CreateMenu();
		return osContinue;
	}

	//return osEnd;
	return osContinue;
}


//////////////////////////////////////////////////////////////////////////////
//	cOsdMenu
//////////////////////////////////////////////////////////////////////////////

menuShow cPlexMenu::eShow = MAIN;

/**
**	Plex menu constructor.
*/
cPlexMenu::cPlexMenu(const char *title, int c0, int c1, int c2, int c3, int c4)
	:cOsdMenu(title)
{
	SetHasHotkeys();

	for(std::vector<plexclient::PlexServer>::iterator it = plexclient::plexgdm::GetInstance().GetPlexservers().begin(); it != plexclient::plexgdm::GetInstance().GetPlexservers().end(); ++it) {
		//&(*it)
		auto s1 = std::make_shared<plexclient::Plexservice>( &(*it) );
		s1->StartUri = "/library/sections";
		Add(new cPlexOsdItem(Poco::format(tr("%s - Library"), it->GetServerName()).c_str(),  s1));

		auto s2 = std::make_shared<plexclient::Plexservice>( &(*it) );
		s2->StartUri = "/video";
		Add(new cPlexOsdItem(Poco::format(tr("%s - Video Channels"), it->GetServerName()).c_str(), s2 ));
	}

	if(Count() < 1) {
		Add(new cPlexOsdItem(tr("No Plex Media Server found.")), false);
	}
}

cOsdMenu* cPlexMenu::ProcessMenu() 
{
	if(cMyPlugin::CalledFromCode) {
		cMyPlugin::CalledFromCode = false;
		return cPlexBrowser::RecoverLastState();
	}
	
	if (cPlexMenu::eShow == menuShow::BROWSER) {
		return new cPlexBrowser(tr("Browse Plex"), pPlexService);
	}
	return new cPlexMenu("Plex");
}


/**
**	Handle play plugin menu key event.
**
**	@param key	key event
*/
eOSState cPlexMenu::ProcessKey(eKeys key)
{
	eOSState state;

	//if (key != kNone) {
	//	dsyslog("[plex]%s: key=%d\n", __FUNCTION__, key);
	//}
	// call standard function
	state = cOsdMenu::ProcessKey(key);

	int current = Current();		// get current menu item index
	cPlexOsdItem *item = static_cast<cPlexOsdItem*>(Get(current));

	switch (state) {
	case osUnknown:
		switch (key) {
		case kOk:
			pPlexService = item->GetAttachedService();
			cPlexMenu::eShow = menuShow::BROWSER;
			return osPlugin;		// restart with OSD browser
		default:
			break;
		}
	default:
		break;
	}
	return state;
}