summaryrefslogtreecommitdiff
path: root/plugins/NewsAggregator/Src/Update.cpp
blob: 5a34bfb69ba68040f3b08491fc2203c5ac485bed (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
/* 
Copyright (C) 2012 Mataes

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

This 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  
*/

#include "common.h"

// check if Feed is currently updating
BOOL ThreadRunning;
UPDATELIST *UpdateListHead = NULL;
UPDATELIST *UpdateListTail = NULL;

// main auto-update timer
VOID CALLBACK timerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
	// only run if it is not current updating and the auto update option is enabled
	if (!ThreadRunning && !Miranda_Terminated())
	{
		BOOL HaveUpdates = FALSE;
		HANDLE hContact = db_find_first();
		while (hContact != NULL) 
		{
			if(IsMyContact(hContact))
			{
				if (DBGetContactSettingDword(hContact, MODULE, "UpdateTime", 60))
				{
					double diff = difftime(time(NULL), DBGetContactSettingDword(hContact, MODULE, "LastCheck", 0));
					if (db_get_b(NULL, MODULE, "AutoUpdate", 1) != 0 && diff >= DBGetContactSettingDword(hContact, MODULE, "UpdateTime", 60) * 60)
					{
						UpdateListAdd(hContact);
						HaveUpdates = TRUE;
					}
				}
			}
			hContact = db_find_next(hContact);
		}
		if (!ThreadRunning && HaveUpdates)
			mir_forkthread(UpdateThreadProc, (LPVOID)FALSE);
	}
}

// temporary timer for first run
// when this is run, it kill the old startup timer and create the permenant one above
VOID CALLBACK timerProc2(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
	KillTimer(NULL, timerId);
	ThreadRunning = FALSE;

	if (db_get_b(NULL, MODULE, "AutoUpdate", 1) && !Miranda_Terminated())
	{
		if (db_get_b(NULL, MODULE, "StartupRetrieve", 1))
			CheckAllFeeds(0, 1);
		timerId = SetTimer(NULL, 0, 30000, (TIMERPROC)timerProc);
	}
}

void UpdateListAdd(HANDLE hContact)
{
	UPDATELIST *newItem;

	newItem = (UPDATELIST*)mir_alloc(sizeof(UPDATELIST));
	newItem->hContact = hContact;
	newItem->next = NULL;

	WaitForSingleObject(hUpdateMutex, INFINITE);

	if (UpdateListTail == NULL)
		UpdateListHead = newItem;
	else UpdateListTail->next = newItem;
	UpdateListTail = newItem;

	ReleaseMutex(hUpdateMutex);
}

HANDLE UpdateGetFirst()
{
	HANDLE hContact = NULL;

	WaitForSingleObject(hUpdateMutex, INFINITE);

	if (UpdateListHead != NULL)
	{
		UPDATELIST* Item = UpdateListHead;

		hContact = Item->hContact;
		UpdateListHead = Item->next;
		mir_free(Item);

		if (UpdateListHead == NULL)
			UpdateListTail = NULL;
	}

	ReleaseMutex(hUpdateMutex);

	return hContact;
}

void DestroyUpdateList(void)
{
	UPDATELIST *temp;

	WaitForSingleObject(hUpdateMutex, INFINITE);

	temp = UpdateListHead;

	// free the list one by one
	while (temp != NULL)
	{
		UpdateListHead = temp->next;
		mir_free(temp);
		temp = UpdateListHead;
	}
	// make sure the entire list is clear
	UpdateListTail = NULL;

	ReleaseMutex(hUpdateMutex);
}

void UpdateThreadProc(LPVOID AvatarCheck)
{
	WaitForSingleObject(hUpdateMutex, INFINITE);
	if (ThreadRunning)
	{
		ReleaseMutex(hUpdateMutex);
		return;
	}
	ThreadRunning = TRUE;	// prevent 2 instance of this thread running
	ReleaseMutex(hUpdateMutex);

	// update news by getting the first station from the queue until the queue is empty
	while (UpdateListHead != NULL && !Miranda_Terminated())
		if ((BOOL)AvatarCheck)
			CheckCurrentFeedAvatar(UpdateGetFirst());
		else
			CheckCurrentFeed(UpdateGetFirst());

	// exit the update thread
	ThreadRunning = FALSE;
}