~elementary-pantheon/contractor/master

« back to all changes in this revision

Viewing changes to src/ContractFile.vala

  • Committer: RabbitBot
  • Author(s): Florian R. A. Angermeier
  • Date: 2015-09-21 13:31:07 UTC
  • mfrom: (136.1.11)
  • Revision ID: git-v1:4a5e95794024146bc89ee39053530c11e4977103
Implement filter functions based on file size:
* Get the max file size (int64, size in bytes) from a .contract file (optional key MaxFileSize)
* Add methods to the D-Bus service:
  - get_contracts_by_file_size
  - get_contracts_by_mime_and_file_size
  - get_contracts_by_mimelist_and_file_size

Add documentation

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
16
 */
17
17
 
 
18
/**
 
19
 * used to access the Contracts File object and read its content
 
20
 */
18
21
public class Contractor.ContractFile : Object {
 
22
    /**
 
23
     * contract files filename extension
 
24
     */
19
25
    private const string EXTENSION = ".contract";
20
26
 
 
27
    /**
 
28
     * the File object used to access its content
 
29
     */
21
30
    private File file;
22
31
 
 
32
    /**
 
33
     * the constructor to create ContractFile object which contains the passed
 
34
     * File object
 
35
     *
 
36
     * @param file the file to contain
 
37
     */
23
38
    public ContractFile (File file) {
24
39
        this.file = file;
25
40
    }
26
41
 
 
42
    /**
 
43
     * get the contract ID from the filename, e.g. file-roller-compress
 
44
     * (file-roller-compress.contract)
 
45
     *
 
46
     * @return the contracts ID, e.g. file-roller-compress
 
47
     */
27
48
    public string get_id () {
28
49
        return remove_extension (file.get_basename ());
29
50
    }
30
51
 
 
52
    /**
 
53
     * loads and returns the internally stored files content
 
54
     *
 
55
     * @return the files content as string
 
56
     */
31
57
    public string get_contents () throws Error {
32
58
        uint8[] file_data;
33
59
 
34
 
        if (file.load_contents (null, out file_data, null))
 
60
        if (file.load_contents (null, out file_data, null)) {
35
61
            return (string) file_data;
 
62
        }
36
63
 
37
64
        return "";
38
65
    }
39
66
 
 
67
    /**
 
68
     * checks if the filename extension is '.contract'
 
69
     *
 
70
     * @param filename the full filename incl. the filename extension
 
71
     *
 
72
     * @return true if the filename extension is '.contract'; false otherwise
 
73
     */
40
74
    public static bool is_valid_filename (string filename) {
41
75
        return filename[- EXTENSION.length : filename.length] == EXTENSION;
42
76
    }
43
77
 
 
78
    /**
 
79
     * removes the filename extension and returns the result
 
80
     *
 
81
     * @param file_name the filename incl. the filename extesnion
 
82
     *
 
83
     * @return the filename without the filename extension
 
84
     */
44
85
    private static string remove_extension (string file_name) {
45
86
        return file_name[0 : - EXTENSION.length];
46
87
    }