File: conflictcheck_thread.c

package info (click to toggle)
vdr-plugin-epgsearch 1.0.1~beta6+git20150211-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,780 kB
  • ctags: 2,325
  • sloc: ansic: 23,325; perl: 1,330; sh: 609; makefile: 197
file content (167 lines) | stat: -rw-r--r-- 5,366 bytes parent folder | download | duplicates (2)
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
/*                                                                  -*- c++ -*-
Copyright (C) 2004-2013 Christian Wieninger

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html

The author can be reached at cwieninger@gmx.de

The project's page is at http://winni.vdr-developer.org/epgsearch
*/

#include <vector>
#include "conflictcheck_thread.h"
#include "epgsearchcfg.h"
#include "mail.h"
#include "epgsearch.h"

#define CONFLICTCHECK_NICE 19

cConflictCheckThread *cConflictCheckThread::m_Instance = NULL;
time_t cConflictCheckThread::m_cacheNextConflict = 0;
int cConflictCheckThread::m_cacheRelevantConflicts = 0;
int cConflictCheckThread::m_cacheTotalConflicts = 0;
bool cConflictCheckThread::m_runOnce = false;
bool cConflictCheckThread::m_forceUpdate = false;

cConflictCheckThread::cConflictCheckThread(cPluginEpgsearch* thePlugin)
: cThread("EPGSearch: conflictcheck")
{
    m_plugin = thePlugin;
    m_Active = false;
    m_lastUpdate = time(NULL);
    m_runOnce = false;
    m_forceUpdate = false;
}

cConflictCheckThread::~cConflictCheckThread() {
    if (m_Active)
	Stop();
}

void cConflictCheckThread::Init(cPluginEpgsearch* thePlugin, bool runOnce)
{
   if (EPGSearchConfig.checkTimerConflictsAfterUpdate || EPGSearchConfig.conflictCheckIntervall == 0)
   {
      if (!runOnce) return;
      m_runOnce = true;
   }

   if (m_Instance == NULL) {
      m_Instance = new cConflictCheckThread(thePlugin);
      m_Instance->Start();
   }
   else
      if (runOnce) m_forceUpdate = true; // force an update, because thread is already running

}

void cConflictCheckThread::Exit(void) {
    if (m_Instance != NULL) {
	m_Instance->Stop();
	DELETENULL(m_Instance);
    }
}

void cConflictCheckThread::Stop(void) {
    m_Active = false;
    Wait.Signal();
    Cancel(6);
}

void cConflictCheckThread::Action(void)
{
    SetPriority(CONFLICTCHECK_NICE);

    m_Active = true;
    // let VDR do its startup
    if (!m_runOnce)
      {
	if (!cPluginEpgsearch::VDR_readyafterStartup)
	  LogFile.Log(2, "ConflictCheckThread: waiting for VDR to become ready...");
	while(m_Active && !cPluginEpgsearch::VDR_readyafterStartup)
	  Wait.Wait(1000);
      }

    time_t nextUpdate = time(NULL);
    while (m_Active && Running())
    {
	time_t now = time(NULL);
	if (now >= nextUpdate || m_forceUpdate)
	{
	    m_forceUpdate = false;
	    if (Timers.BeingEdited())
	    {
		Wait.Wait(1000);
		continue;
	    }
	    LogFile.iSysLog("timer conflict check started");

	    cConflictCheck conflictCheck;
	    conflictCheck.Check();

	    time_t nextConflict = 0;
	    if (conflictCheck.relevantConflicts > 0)
	    {
	      cString msgfmt = "";
	      if (conflictCheck.relevantConflicts == 1)
		msgfmt = cString::sprintf(tr("timer conflict at %s! Show it?"),
						*DateTime(conflictCheck.nextRelevantConflictDate));
	      else
		msgfmt = cString::sprintf(tr("%d timer conflicts! First at %s. Show them?"),
						conflictCheck.relevantConflicts,
						*DateTime(conflictCheck.nextRelevantConflictDate));
	      bool doMessage = EPGSearchConfig.noConflMsgWhileReplay == 0 ||
		!cDevice::PrimaryDevice()->Replaying() ||
		conflictCheck.nextRelevantConflictDate - now < 2*60*60;
	      if (doMessage && SendMsg(msgfmt, true,7, mtWarning) == kOk)
		{
		  m_plugin->showConflicts = true;
		  cRemote::CallPlugin("epgsearch");
		}

	      if (EPGSearchConfig.sendMailOnConflicts)
		{
		  cMailConflictNotifier mailNotifier;
		  mailNotifier.SendConflictNotifications(conflictCheck);
		}
	      conflictCheck.EvaluateConflCheckCmd();
	    }
	    // store for external access
	    cConflictCheckThread::m_cacheNextConflict = conflictCheck.nextRelevantConflictDate;
	    cConflictCheckThread::m_cacheRelevantConflicts = conflictCheck.relevantConflicts;
	    cConflictCheckThread::m_cacheTotalConflicts = conflictCheck.numConflicts;

	    LogFile.iSysLog("timer conflict check finished");

	    m_lastUpdate = time(NULL);
	    int Intervall = EPGSearchConfig.conflictCheckIntervall;
	    if (nextConflict > 0 && EPGSearchConfig.conflictCheckWithinLimit > 0 &&
		nextConflict - time(NULL) < EPGSearchConfig.conflictCheckWithinLimit * 60)
		Intervall = EPGSearchConfig.conflictCheckIntervall2;

	    nextUpdate = long(m_lastUpdate/60)*60 + (Intervall * 60);
	}
	if (m_Active && Running())
	    Wait.Wait(2000); // to avoid high system load if time%30==0 ?????????????????????
	// no waiting in the while loop if m_runOnce is true
	while (Running() && m_Active && time(NULL)%30 != 0 && !m_runOnce) // sync heart beat to a multiple of 5secs
	    Wait.Wait(1000);
    };

    m_Active = false;	// always false at this point
    LogFile.iSysLog("Leaving conflict check thread");
}