summaryrefslogtreecommitdiff
path: root/plugins/FTPFileYM/src/dbentry.cpp
blob: f5c5684327a5f10b8d513bdfd3464f5f92fa7043 (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
/*
FTP File YM plugin
Copyright (C) 2007-2010 Jan Holub

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 version 2
of the License.

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, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

int DBEntry::entryID;
mir_cs DBEntry::mutexDB;

DBEntry::DBEntry()
{
}

DBEntry::DBEntry(DBEntry *entry)
{
	m_fileID = entry->m_fileID;
	m_iFtpNum = entry->m_iFtpNum;
	strncpy_s(m_szFileName, entry->m_szFileName, _TRUNCATE);
}

DBEntry* DBEntry::getFirst()
{
	entryID = 0;
	DBEntry *entry = new DBEntry();
	return getNext(entry);
}

DBEntry *DBEntry::getNext(DBEntry *entry)
{
	char szValue[256];
	int count = db_get_dw(0, MODULE_FILES, "NextFileID", 0);
	
	CMStringA frmt;
	for (; entryID < count; entryID++) { 
		int ftpNum = DB::getByteF(0, MODULE_FILES, "Ftp%d", entryID, -1);
		if (ftpNum != -1) {
			ptrA Value(db_get_sa(NULL, MODULE, frmt.Format("Filename%d", entryID)));
			if (Value) {
				strncpy_s(szValue, Value, _TRUNCATE);
				entry->m_fileID = entryID;
				entry->m_iFtpNum = ftpNum;
				strncpy_s(entry->m_szFileName, szValue, _TRUNCATE);
				entry->m_deleteTS = DB::getDwordF(0, MODULE_FILES, "DeleteTS%d", entryID, 0);
				entryID++;
				return entry;
			}
		}
	}

	delete entry;
	return nullptr;
}

void DBEntry::cleanupDB()
{
	int count = 0;

	DBEntry *entry = getFirst();
	while (entry != nullptr) {
		DB::setByteF(0, MODULE_FILES, "Ftp%d", count, entry->m_iFtpNum);
		DB::setAStringF(0, MODULE_FILES, "Filename%d", count, entry->m_szFileName);
		if (entry->m_deleteTS != 0)
			DB::setDwordF(0, MODULE_FILES, "DeleteTS%d", count, entry->m_deleteTS);

		count++;
		entry = getNext(entry);
	}

	db_set_dw(0, MODULE_FILES, "NextFileID", count);
}

DBEntry* DBEntry::get(int fileID)
{
	char szValue[256];
	DBEntry *entry = new DBEntry();

	int ftpNum = DB::getByteF(0, MODULE_FILES, "Ftp%d", fileID, -1);
	if (ftpNum != -1) {
		ptrA Value(db_get_sa(NULL, MODULE, CMStringA(FORMAT, "Filename%d", fileID)));
		if (Value) {
			strncpy_s(szValue, Value, _TRUNCATE);
			entry->m_fileID = fileID;
			entry->m_iFtpNum = ftpNum;
			strncpy_s(entry->m_szFileName, szValue, _TRUNCATE);
			entry->m_deleteTS = DB::getDwordF(0, MODULE_FILES, "DeleteTS%d", fileID, 0);
			return entry;
		}
	}

	return nullptr;
}

void DBEntry::remove(int fileID)
{
	DB::deleteSettingF(0, MODULE_FILES, "Ftp%d", fileID);
	DB::deleteSettingF(0, MODULE_FILES, "Filename%d", fileID);
	DB::deleteSettingF(0, MODULE_FILES, "DeleteTS%d", fileID);
}

bool DBEntry::entryExists(GenericJob *job)
{
	mir_cslock lock(mutexDB);

	DBEntry *entry = getFirst();
	while (entry != nullptr) {
		if (entry->m_iFtpNum == job->m_iFtpNum && !strcmp(entry->m_szFileName, job->m_szSafeFileName))
			return true;

		entry = getNext(entry);
	}

	return false;
}

void DBEntry::add(GenericJob *job)
{
	if (entryExists(job))
		return;

	mir_cslock lock(mutexDB);
	int id = db_get_dw(0, MODULE_FILES, "NextFileID", 0);
	DB::setByteF(0, MODULE_FILES, "Ftp%d", id, job->m_iFtpNum);
	DB::setAStringF(0, MODULE_FILES, "Filename%d", id, job->m_szSafeFileName);

	if (job->m_tab->m_iOptAutoDelete != -1) {
		time_t deleteTS = time(nullptr);
		deleteTS += (job->m_tab->m_iOptAutoDelete * 60);
		DB::setDwordF(0, MODULE_FILES, "DeleteTS%d", id, deleteTS);
	}

	db_set_dw(0, MODULE_FILES, "NextFileID", id + 1);
	job->m_fileID = id;
}

void DBEntry::setDeleteTS(GenericJob *job)
{
	if (job->m_tab->m_iOptAutoDelete != -1) {
		time_t deleteTS = time(nullptr);
		deleteTS += (job->m_tab->m_iOptAutoDelete * 60);
		DB::setDwordF(0, MODULE_FILES, "DeleteTS%d", job->m_fileID, deleteTS);
	}
	else DB::deleteSettingF(0, MODULE_FILES, "DeleteTS%d", job->m_fileID);
}