Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Structs.vala « src - github.com/jangernert/FeedReader.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f0ab106a1f0c5a9ea1f2743c25bb92605974594a (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
158
159
160
161
162
163
164
165
166
167
//	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/>.

namespace FeedReader {

	public struct BackendInfo {
		string ID;
		string name;
		BackendFlags flags;
		string website;
		string iconName;
	}

	public struct Response {
		uint status;
		string data;
		Soup.MessageHeaders headers;

		public bool is_ok()
		{
			return status >= 200 && status < 400;
		}
	}

	public struct ResourceMetadata
	{
		private const string CACHE_GROUP = "cache";
		private const string ETAG_KEY = "etag";
		private const string LAST_MODIFIED_KEY = "last_modified";
		private const string EXPIRES_KEY = "last_checked";

		string? etag;
		string? last_modified;
		DateTime? expires;

		public ResourceMetadata()
		{
		}

		public ResourceMetadata.from_data(string data)
		{
			try
			{
				var config = new KeyFile();
				config.load_from_data(data, data.length, KeyFileFlags.NONE);
				try { this.etag = config.get_string(CACHE_GROUP, ETAG_KEY); }
				catch (KeyFileError.KEY_NOT_FOUND e) {}
				catch (KeyFileError.GROUP_NOT_FOUND e) {}
				try { this.last_modified = config.get_string(CACHE_GROUP, LAST_MODIFIED_KEY); }
				catch (KeyFileError.KEY_NOT_FOUND e) {}
				catch (KeyFileError.GROUP_NOT_FOUND e) {}

				int64? expires = null;
				try { expires = config.get_int64(CACHE_GROUP, EXPIRES_KEY); }
				catch (KeyFileError.KEY_NOT_FOUND e) {}
				catch (KeyFileError.GROUP_NOT_FOUND e) {}
				if(expires != null)
				{
					this.expires = new DateTime.from_unix_utc(expires);
				}
			}
			catch (KeyFileError e)
			{
				Logger.warning(@"FaviconMetadata.from_file: Failed to load from $data");
			}
		}

		public static async ResourceMetadata from_file_async(string filename)
		{
			try
			{
				var file = File.new_for_path(filename);
				uint8[] contents;
				yield file.load_contents_async(null, out contents, null);
				return ResourceMetadata.from_data((string)contents);
			}
			catch (IOError.NOT_FOUND e)
			{
			}
			catch (Error e)
			{
				Logger.warning(@"FaviconMetadata.from_file: Failed to load $filename: " + e.message);
			}
			return ResourceMetadata();
		}

		public async void save_to_file_async(string filename)
		{
			var file = File.new_for_path(filename);
			if(this.etag == null && this.last_modified == null && this.expires == null)
			{
				try
				{
					yield file.delete_async();
				}
				catch (IOError.NOT_FOUND e)
				{
				}
				catch (Error e)
				{
					Logger.warning(@"FaviconMetadata.save_to_file: Error deleting metadata file $filename: " + e.message);
				}
			}
			else
			{
				var config = new KeyFile();
				if(this.etag != null)
				{
					config.set_string(CACHE_GROUP, ETAG_KEY, this.etag);
				}
				if(this.last_modified != null)
				{
					config.set_string(CACHE_GROUP, LAST_MODIFIED_KEY, this.last_modified);
				}
				if(this.expires != null)
				{
					config.set_int64(CACHE_GROUP, EXPIRES_KEY, this.expires.to_unix());
				}
				var data = config.to_data();
				try
				{
					try
					{
						file.get_parent().make_directory_with_parents();
					}
					catch (Error e)
					{
						// don't care if the folder already exists
					}
					yield file.replace_contents_async(data.data, null, false, FileCreateFlags.NONE, null, null);
				}
				catch (Error e)
				{
					Logger.warning(@"FaviconMetadata.save_to_file: Failed to save metadata file $filename,  $data:\n" + e.message);
				}
			}
		}

		public bool is_expired()
		{
			if(expires == null)
			{
				return true;
			}

			if(expires.compare(new DateTime.now_utc()) == 1)
			{
				return false;
			}

			return true;
		}
	}

}