File: ViewHistory.cpp

package info (click to toggle)
pinot 1.05-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,848 kB
  • ctags: 3,572
  • sloc: cpp: 39,255; sh: 10,481; ansic: 3,049; makefile: 620; xml: 379
file content (180 lines) | stat: -rw-r--r-- 4,022 bytes parent folder | download | duplicates (3)
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
/*
 *  Copyright 2005-2013 Fabrice Colin
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <cstdlib>

#include "Url.h"
#include "ViewHistory.h"

using std::clog;
using std::endl;
using std::string;

ViewHistory::ViewHistory(const string &database) :
	SQLiteBase(database)
{
}

ViewHistory::~ViewHistory()
{
}

/// Creates the ViewHistory table in the database.
bool ViewHistory::create(const string &database)
{
	bool createTable = false;

	// The specified path must be a file
	if (SQLiteBase::check(database) == false)
	{
		return false;
	}

	SQLiteBase db(database);

	// Does ViewHistory exist ?
	if (db.executeSimpleStatement("SELECT * FROM ViewHistory LIMIT 1;") == false)
	{
#ifdef DEBUG
		clog << "ViewHistory::create: ViewHistory doesn't exist" << endl;
#endif
		createTable = true;
	}
	else
	{
		// Previous versions didn't include a Date field, so check for it
		if (db.executeSimpleStatement("SELECT Date FROM ViewHistory LIMIT 1;") == false)
		{
#ifdef DEBUG
			clog << "ViewHistory::create: ViewHistory needs updating" << endl;
#endif
			// Ideally, we would use ALTER TABLE but it's not supported by SQLite
			if (db.executeSimpleStatement("DROP TABLE ViewHistory; VACUUM;") == true)
			{
				createTable = true;
			}
		}
	}

	// Create the table ?
	if (createTable == true)
	{
		if (db.executeSimpleStatement("CREATE TABLE ViewHistory (Url VARCHAR(255) \
			PRIMARY KEY, Status INTEGER, DATE INTEGER);") == false)
		{
			return false;
		}
	}

	return true;
}

/// Inserts an URL.
bool ViewHistory::insertItem(const string &url)
{
	bool success = false;

	SQLResults *results = executeStatement("INSERT INTO ViewHistory \
		VALUES('%q', '1', '%d');", Url::escapeUrl(url).c_str(), time(NULL));
	if (results != NULL)
	{
		success = true;
		delete results;
	}

	return success;
}

/// Checks if an URL is in the history.
bool ViewHistory::hasItem(const string &url)
{
	bool success = false;

	SQLResults *results = executeStatement("SELECT Url FROM ViewHistory \
		WHERE Url='%q';", Url::escapeUrl(url).c_str());
	if (results != NULL)
	{
		SQLRow *row = results->nextRow();
		if (row != NULL)
		{
			// If this returns anything, it's the URL we are looking for
#ifdef DEBUG
			clog << "ViewHistory::hasItem: URL " << row->getColumn(0) << endl;
#endif
			success = true;

			delete row;
		}

		delete results;
	}

	return success;
}

/// Returns the number of items.
unsigned int ViewHistory::getItemsCount(void)
{
	unsigned int count = 0;

	SQLResults *results = executeStatement("SELECT COUNT(*) FROM ViewHistory;");
	if (results != NULL)
	{
		count = (unsigned int)results->getIntCount();

		delete results;
	}

	return count;
}

/// Deletes an URL.
bool ViewHistory::deleteItem(const string &url)
{
	bool success = false;

	SQLResults *results = executeStatement("DELETE FROM ViewHistory \
		WHERE Url='%q';", Url::escapeUrl(url).c_str());
	if (results != NULL)
	{
		success = true;
		delete results;
	}

	return success;
}

/// Expires items older than the given date.
bool ViewHistory::expireItems(time_t expiryDate)
{
	bool success = false;

	SQLResults *results = executeStatement("DELETE FROM ViewHistory \
		WHERE Date<'%d';", expiryDate);
	if (results != NULL)
	{
		success = true;
		delete results;
	}

	return success;
}