File: CachedActionManager.vala

package info (click to toggle)
feedreader 2.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,868 kB
  • sloc: ansic: 11,829; xml: 879; php: 170; sh: 32; makefile: 16; python: 14
file content (163 lines) | stat: -rw-r--r-- 4,140 bytes parent folder | download
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
//	This file is part of FeedReader.
//
//	FeedReader 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 3 of the License, or
//	(at your option) any later version.
//
//	FeedReader 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 FeedReader.  If not, see <http://www.gnu.org/licenses/>.

public class FeedReader.CachedActionManager : GLib.Object {

private CachedActions m_lastAction = CachedActions.NONE;
private string m_ids = "";

private static CachedActionManager? m_manager = null;

public static CachedActionManager get_default()
{
	if(m_manager == null)
		m_manager = new CachedActionManager();

	return m_manager;
}

private CachedActionManager()
{

}


public void markArticleRead(string id, ArticleStatus read)
{
	var cachedAction = CachedActions.MARK_READ;
	if(read == ArticleStatus.UNREAD)
		cachedAction = CachedActions.MARK_UNREAD;

	var action = new CachedAction(cachedAction, id, "");
	addAction(action);
}

public void markArticleStarred(string id, ArticleStatus marked)
{
	var cachedAction = CachedActions.MARK_STARRED;
	if(marked == ArticleStatus.UNMARKED)
		cachedAction = CachedActions.MARK_UNSTARRED;

	var action = new CachedAction(cachedAction, id, "");
	addAction(action);
}

public void markFeedRead(string id)
{
	var action = new CachedAction(CachedActions.MARK_READ_FEED, id, "");
	addAction(action);
}

public void markCategoryRead(string id)
{
	var action = new CachedAction(CachedActions.MARK_READ_CATEGORY, id, "");
	addAction(action);
}

public void markAllRead()
{
	var action = new CachedAction(CachedActions.MARK_READ_ALL, "", "");
	addAction(action);
}

private void addAction(CachedAction action)
{
	var db = DataBase.writeAccess();
	if(db.cachedActionNecessary(action))
	{
		db.addCachedAction(action.getType(), action.getID());
	}
	else
	{
		db.deleteOppositeCachedAction(action);
	}
}

public void executeActions()
{
	var db = DataBase.writeAccess();
	if(db.isTableEmpty("CachedActions"))
	{
		Logger.debug("CachedActionManager - executeActions: no actions to perform");
		return;
	}


	Logger.debug("CachedActionManager: executeActions");

	var actions = db.readCachedActions();

	foreach(CachedAction action in actions)
	{
		Logger.debug("CachedActionManager: executeActions %s %s".printf(action.getID(), action.getType().to_string()));
		switch(action.getType())
		{
		case CachedActions.MARK_READ:
		case CachedActions.MARK_UNREAD:
			if(action.getType() != m_lastAction && m_ids != "")
			{
				m_ids += action.getID();
				execute(m_ids.substring(1), m_lastAction);
				m_lastAction = CachedActions.NONE;
				m_ids = "";
			}
			else
			{
				m_ids += "," + action.getID();
			}
			break;
		case CachedActions.MARK_STARRED:
			FeedServer.get_default().setArticleIsMarked(action.getID(), ArticleStatus.MARKED);
			break;
		case CachedActions.MARK_UNSTARRED:
			FeedServer.get_default().setArticleIsMarked(action.getID(), ArticleStatus.UNMARKED);
			break;
		case CachedActions.MARK_READ_FEED:
			FeedServer.get_default().setFeedRead(action.getID());
			break;
		case CachedActions.MARK_READ_CATEGORY:
			FeedServer.get_default().setCategoryRead(action.getID());
			break;
		case CachedActions.MARK_READ_ALL:
			FeedServer.get_default().markAllItemsRead();
			break;
		}

		m_lastAction = action.getType();
	}

	if(m_ids != "")
	{
		execute(m_ids.substring(1), m_lastAction);
	}

	db.resetCachedActions();
}

private void execute(string ids, CachedActions action)
{
	Logger.debug("CachedActionManager: execute %s %s".printf(ids, action.to_string()));
	switch(action)
	{
	case CachedActions.MARK_READ:
		FeedServer.get_default().setArticleIsRead(ids, ArticleStatus.READ);
		break;
	case CachedActions.MARK_UNREAD:
		FeedServer.get_default().setArticleIsRead(ids, ArticleStatus.UNREAD);
		break;
	}
}

}