| 1 | /* i4ucproject.vala |
|---|
| 2 | * |
|---|
| 3 | * Copyright (C) 2009-2010 Matias De la Puente |
|---|
| 4 | * |
|---|
| 5 | * This program is free software: you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | * |
|---|
| 18 | * Author: |
|---|
| 19 | * Matias De la Puente <mfpuente.ar@gmail.com> |
|---|
| 20 | */ |
|---|
| 21 | using Gee; |
|---|
| 22 | |
|---|
| 23 | public class I4uc.Project : GLib.Object |
|---|
| 24 | { |
|---|
| 25 | private const string _I4UC_PROJECT = "i4uc project"; |
|---|
| 26 | |
|---|
| 27 | private Document _project_file = new Document (); |
|---|
| 28 | private ArrayList<string> _authors = new ArrayList<string> (); |
|---|
| 29 | private ArrayList<string> _files = new ArrayList<string> (); |
|---|
| 30 | private HashMap<string, ProjectProfile> _profiles = new HashMap<string, ProjectProfile> (); |
|---|
| 31 | |
|---|
| 32 | public string name { set; get; } |
|---|
| 33 | public string uri { owned get { return _project_file.uri; } } |
|---|
| 34 | public string folder_uri { owned get { return _project_file.folder; } } |
|---|
| 35 | public Gee.List<string> authors { get { return _authors; } } |
|---|
| 36 | public Gee.List<string> files { get { return _files; } } |
|---|
| 37 | public Gee.Map<string, ProjectProfile> profiles { get { return _profiles; } } |
|---|
| 38 | |
|---|
| 39 | public bool open (string uri) throws GLib.Error, GLib.KeyFileError |
|---|
| 40 | { |
|---|
| 41 | var project_content = _project_file.load_contents (uri); |
|---|
| 42 | var key_file = new KeyFile (); |
|---|
| 43 | if (!key_file.load_from_data (project_content, project_content.length, KeyFileFlags.NONE) || |
|---|
| 44 | key_file.get_start_group () != _I4UC_PROJECT) |
|---|
| 45 | return false; |
|---|
| 46 | |
|---|
| 47 | this.name = key_file.get_string (_I4UC_PROJECT, "name"); |
|---|
| 48 | |
|---|
| 49 | var authors = key_file.get_string_list (_I4UC_PROJECT, "authors"); |
|---|
| 50 | _authors.clear (); |
|---|
| 51 | foreach (var author in authors) |
|---|
| 52 | _authors.add (author); |
|---|
| 53 | |
|---|
| 54 | var files = key_file.get_string_list (_I4UC_PROJECT, "files"); |
|---|
| 55 | _files.clear (); |
|---|
| 56 | foreach (var file in files) |
|---|
| 57 | _files.add (file); |
|---|
| 58 | |
|---|
| 59 | _profiles.clear (); |
|---|
| 60 | if (key_file.has_key (_I4UC_PROJECT, "profiles")) |
|---|
| 61 | { |
|---|
| 62 | var profiles = key_file.get_string_list (_I4UC_PROJECT, "profiles"); |
|---|
| 63 | foreach (var profile in profiles) |
|---|
| 64 | if (key_file.has_group (@"$profile profile")) |
|---|
| 65 | _profiles[profile] = new ProjectProfile.from_key_file (key_file, @"$profile profile"); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | return true; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | public void save (string? uri = null) |
|---|
| 72 | { |
|---|
| 73 | var key_file = new KeyFile (); |
|---|
| 74 | |
|---|
| 75 | key_file.set_string (_I4UC_PROJECT, "name", _name); |
|---|
| 76 | key_file.set_string_list (_I4UC_PROJECT, "authors", _authors.to_array ()); |
|---|
| 77 | key_file.set_string_list (_I4UC_PROJECT, "files", _files.to_array ()); |
|---|
| 78 | key_file.set_string_list (_I4UC_PROJECT, "profiles", _profiles.keys.to_array ()); |
|---|
| 79 | |
|---|
| 80 | foreach (var profile in _profiles.values) |
|---|
| 81 | profile.to_key_file (key_file, @"$(profile.name) profile"); |
|---|
| 82 | |
|---|
| 83 | _project_file.save_contents (key_file.to_data (null), uri); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | public string? get_file_uri (string file) |
|---|
| 87 | { |
|---|
| 88 | return File.new_for_uri (this.folder_uri).get_child_for_display_name (file).get_uri (); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|