<%pre>

#include <timers.h>
#include <tools.h>
#include <setup.h>
#include <epg_events.h>
#include <timerconflict.h>
#include <livefeatures.h>
#include <users.h>

using namespace vdrlive;

static const size_t maximumDescriptionLength = 300;

</%pre>
<%args>
	// input parameters
	std::string timerid;
	std::string action;
</%args>
<%session scope="global">
	bool logged_in(false);
</%session>
<%request scope="page">
	const cTimer* timer;
	TimerConflictNotifier timerNotifier;
</%request>
<%include>page_init.eh</%include>
<%cpp>
	if (!logged_in && LiveSetup().UseAuth()) return reply.redirect("login.html");

	pageTitle = trVDR("Timers");

	cMutexLock timersLock( &LiveTimerManager() );
	SortedTimers& timers = LiveTimerManager().GetTimers();

	timer = 0;
	if ( !timerid.empty() ) {
		std::string tId = SortedTimers::DecodeDomId(timerid);
//		dsyslog("live: DEBUG: TIMER: tId = %s", tId.c_str());
		timer = timers.GetByTimerId(tId);
		if ( timer == 0 )
			throw HtmlError( tr("Couldn't find timer. Maybe you mistyped your request?") );
		if (action == "delete") {
			if (!cUser::CurrentUserHasRightTo(UR_DELTIMERS))
				throw HtmlError( tr("Sorry, no permission. Please contact your administrator!") );
//                        dsyslog("live: timers.ecpp timer->Id() %d", timer->Id());
//                        dsyslog("live: timers.ecpp timer->Remote() %s", timer->Remote());
			LiveTimerManager().DelTimer(timer->Id(), timer->Remote());
			timerNotifier.SetTimerModification();
			return reply.redirect("timers.html");
		}
		if (action == "toggle") {
			LiveTimerManager().ToggleTimerActive(timer->Id(), timer->Remote());
			timerNotifier.SetTimerModification();
			return reply.redirect("timers.html");
		}
	}

	std::string previousDay = "";
</%cpp>
<& pageelems.doc_type &>
<html>
	<head>
		<title>VDR-Live - <$ pageTitle $></title>
		<& pageelems.stylesheets &>
		<& pageelems.ajax_js &>
	</head>
	<body onload="adjustHeader()" onresize="adjustHeader()">
		<& pageelems.logo &>
		<& menu active=("timers") component=("timers.timer_actions")>
		<div class="inhalt">
<%cpp>
		#ifdef DEBUG_LOCK
                        dsyslog("live: pages/timers.ecpp LOCK_TIMERS_READ");
                #endif
		        LOCK_TIMERS_READ;
                        cSortedTimers sortedTimers(Timers);
			if (sortedTimers.Size() == 0) {
</%cpp>
				<$ tr("No timer defined") $>
%			} else {
			<table class="listing" cellspacing="0" cellpadding="0">
<%cpp>
				// output of the timer list:
                                for (int i = 0; i < sortedTimers.Size(); i++) {
                                        const cTimer *timer = sortedTimers[i];
	                 		EpgInfoPtr epgEvent;
					std::string longDescription;
					std::string searchTimName;
					std::string searchTimId;

					if (timer->Event())
					{
						epgEvent = EpgEvents::CreateEpgInfo(timer->Channel(), timer->Event());
						longDescription = StringEscapeAndBreak(SortedTimers::GetTimerInfo(*timer)) + "<hr>"
											+ StringEscapeAndBreak(StringWordTruncate(epgEvent->LongDescr(), maximumDescriptionLength))
											+ "<br/><br/>" + tr("Click to view details.");
						searchTimName = SortedTimers::SearchTimerInfo(*timer, "searchtimer");
						searchTimId = SortedTimers::SearchTimerInfo(*timer, "s-id");
					}
					std::string currentDay = SortedTimers::GetTimerDays(timer);
                                        const cTimer *nextTimer =  NULL;
                                        if (i < (sortedTimers.Size() - 1)) nextTimer = sortedTimers[i + 1];
					bool bottom = false;
					if (i == sortedTimers.Size() - 1) bottom = true;
					else {
						std::string nextDay = SortedTimers::GetTimerDays(nextTimer);
						bottom = (currentDay != nextDay);
					}
					if (previousDay != currentDay) {
						if (!previousDay.empty()) {
</%cpp>
						<tr class="spacer">
							<td colspan="8"/>
						</tr>
<%cpp>
						}
						previousDay = currentDay;
</%cpp>
				<tr class="head">
					<td colspan="10">
						<div class="boxheader"><div><div>
							<$ currentDay $>
						</div></div></div>
					</td>
				</tr>
				<tr class="description">
					<td class="leftcol"><img src="img/transparent.png" alt="" width="16px" height="16px" /></td>
					<td><div class="withmargin"><$ trVDR("Channel") $></div></td>
					<td><div class="withmargin"><$ trVDR("Start") $></div></td>
					<td><div class="withmargin"><$ trVDR("Stop") $></div></td>
					<td><div class="withmargin"><$ trVDR("File") $></div></td>
					<td><div class="withmargin"><$ trVDR("Record on") $></div></td>
					<td><div class="withmargin"><$ tr("Searchtimer") $></div></td>
					<td class="action"><img src="img/transparent.png" alt="" width="16px" height="16px" /></td>
					<td class="action"><img src="img/transparent.png" alt="" width="16px" height="16px" /></td>
					<td class="action rightcol"><img src="img/transparent.png" alt="" width="16px" height="16px" /></td>
				</tr>
<%cpp>
					}
					std::string timerStateImg = "transparent.png";
					std::string timerStateHint;
					if (timer->Recording()) {
						timerStateImg = "arrow_rec.gif";
						timerStateHint = tr("Timer is recording.");
					}
					else if (timer->Flags() & tfActive) {
						timerStateImg = "arrow.png";
						timerStateHint = tr("Timer is active.");
					}
</%cpp>
				<tr>

				<td class="leftcol <? bottom ? "bottomrow" ?>"><img src="<$ LiveSetup().GetThemedLink("img", timerStateImg) $>" alt="" <%cpp> if (!timerStateHint.empty()) { </%cpp><& tooltip.hint text=(timerStateHint) &><%cpp> } </%cpp>></img></td>
					<td class="<? bottom ? "bottomrow" ?>"><div class="withmargin"><a href="schedule.html?channel=<$ timer->Channel()->Number()$>"><$ timer->Channel()->Name() $></a></div></td>
					<td class="<? bottom ? "bottomrow" ?>"><div class="withmargin nowrap"><$ FormatDateTime(tr("%I:%M %p"), timer->StartTime()) $></div></td>
					<td class="<? bottom ? "bottomrow" ?>"><div class="withmargin nowrap"><$ FormatDateTime(tr("%I:%M %p"), timer->StopTime()) $></div></td>
					<td class="<? bottom ? "bottomrow" ?>"><div class="withmargin"><a
%					if (!longDescription.empty()) {
						<& tooltip.hint text=(longDescription) &><& tooltip.display domId=(epgEvent->Id()) &>
%					}
						><$ timer->File() $></a></div>
					</td>
					<td class="<? bottom ? "bottomrow" ?>"><div class="withmargin"><$ timer->Local()?trVDR(" "):timer->Remote() $></div></td>
                                        <td class="<? bottom ? "bottomrow" ?>"><div class="withmargin">
%					if ( !timer->Local()) {
                                        <$ searchTimName $></div></td>
%					} else {
					<a href="edit_searchtimer.html?searchtimerid=<$ searchTimId $>"><$ searchTimName $></a></div></td>
%					}
				        <td class="action <? bottom ? "bottomrow" ?>"><a href="timers.html?timerid=<$ SortedTimers::EncodeDomId(timers.GetTimerId(*timer)) $>&action=toggle"><img src="<$ LiveSetup().GetThemedLink("img", (timer->Flags() & tfActive) ? "active.png" : "inactive.png") $>" alt="" <& tooltip.hint text=(tr("Toggle timer active/inactive")) &>></img></a></td>
			                <td class="action <? bottom ? "bottomrow" ?>"><& pageelems.edit_timer timerId=(timers.GetTimerId(*timer)) &></td>
				        <td class="action rightcol <? bottom ? "bottomrow" ?>"><a href="timers.html?timerid=<$ SortedTimers::EncodeDomId(timers.GetTimerId(*timer)) $>&action=delete"><img src="<$ LiveSetup().GetThemedLink("img", "del.png") $>" alt="" <& tooltip.hint text=(tr("Delete timer")) &>></img></a></td>
				</tr>
<%cpp>
				}
			}
</%cpp>
			</table>
		</div>
	</body>
</html>
<%include>page_exit.eh</%include>

<%def timer_actions>
<a href="edit_timer.html"><$ tr("New timer") $></a>
% if ( LiveFeatures< features::epgsearch >().Recent() ) {
<span class="sep">|</span>
<a href="timerconflicts.html"><$ tr("Timer conflicts") $></a>
% }
</%def>
