IMDb plaintext downloader: fix progress indicator
[cinaest] / src / imdb / imdb-gzip-parser.vala
1 class IMDbGzipParser {
2         private LineParser parser;
3         private Cancellable cancellable;
4
5         public IMDbGzipParser (Cancellable? _cancellable) {
6                 cancellable = _cancellable;
7         }
8
9         public void parse (string path, LineParser? _parser) throws IOError {
10                 parser = _parser;
11
12                 var file = File.new_for_path (path);
13                 var info = file.query_info (FILE_ATTRIBUTE_STANDARD_SIZE, FileQueryInfoFlags.NONE, cancellable);
14                 int total = (int) info.get_size ();
15                 var gz_stream = new GzipInputStream (file.read (cancellable));
16                 var stream = new DataInputStream (gz_stream);
17
18                 int total_in = 0;
19                 size_t length;
20                 string line;
21                 progress (0, 0);
22                 line = stream.read_line (out length, cancellable);
23                 while (line != null) {
24                         parser.parse_line (line);
25                         line = stream.read_line (out length, cancellable);
26                         if (gz_stream.total_in () > total_in) {
27                                 total_in = (int) gz_stream.total_in ();
28                                 progress (total, total_in);
29                         }
30                 }
31         }
32
33         public signal void progress (int total, int now);
34 }