1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * d-dazzle 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with d-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.DirectoryReaper;
20 
21 private import dazzle.c.functions;
22 public  import dazzle.c.types;
23 private import gio.AsyncResultIF;
24 private import gio.Cancellable;
25 private import gio.FileIF;
26 private import glib.ConstructionException;
27 private import glib.ErrorG;
28 private import glib.GException;
29 private import glib.Str;
30 private import gobject.ObjectG;
31 private import gobject.Signals;
32 private import std.algorithm;
33 
34 
35 /** */
36 public class DirectoryReaper : ObjectG
37 {
38 	/** the main Gtk struct */
39 	protected DzlDirectoryReaper* dzlDirectoryReaper;
40 
41 	/** Get the main Gtk struct */
42 	public DzlDirectoryReaper* getDirectoryReaperStruct(bool transferOwnership = false)
43 	{
44 		if (transferOwnership)
45 			ownedRef = false;
46 		return dzlDirectoryReaper;
47 	}
48 
49 	/** the main Gtk struct as a void* */
50 	protected override void* getStruct()
51 	{
52 		return cast(void*)dzlDirectoryReaper;
53 	}
54 
55 	/**
56 	 * Sets our main struct and passes it to the parent class.
57 	 */
58 	public this (DzlDirectoryReaper* dzlDirectoryReaper, bool ownedRef = false)
59 	{
60 		this.dzlDirectoryReaper = dzlDirectoryReaper;
61 		super(cast(GObject*)dzlDirectoryReaper, ownedRef);
62 	}
63 
64 
65 	/** */
66 	public static GType getType()
67 	{
68 		return dzl_directory_reaper_get_type();
69 	}
70 
71 	/** */
72 	public this()
73 	{
74 		auto p = dzl_directory_reaper_new();
75 
76 		if(p is null)
77 		{
78 			throw new ConstructionException("null returned by new");
79 		}
80 
81 		this(cast(DzlDirectoryReaper*) p, true);
82 	}
83 
84 	/** */
85 	public void addDirectory(FileIF directory, GTimeSpan minAge)
86 	{
87 		dzl_directory_reaper_add_directory(dzlDirectoryReaper, (directory is null) ? null : directory.getFileStruct(), minAge);
88 	}
89 
90 	/** */
91 	public void addFile(FileIF file, GTimeSpan minAge)
92 	{
93 		dzl_directory_reaper_add_file(dzlDirectoryReaper, (file is null) ? null : file.getFileStruct(), minAge);
94 	}
95 
96 	/** */
97 	public void addGlob(FileIF directory, string glob, GTimeSpan minAge)
98 	{
99 		dzl_directory_reaper_add_glob(dzlDirectoryReaper, (directory is null) ? null : directory.getFileStruct(), Str.toStringz(glob), minAge);
100 	}
101 
102 	/** */
103 	public bool execute(Cancellable cancellable)
104 	{
105 		GError* err = null;
106 
107 		auto p = dzl_directory_reaper_execute(dzlDirectoryReaper, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
108 
109 		if (err !is null)
110 		{
111 			throw new GException( new ErrorG(err) );
112 		}
113 
114 		return p;
115 	}
116 
117 	/** */
118 	public void executeAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
119 	{
120 		dzl_directory_reaper_execute_async(dzlDirectoryReaper, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
121 	}
122 
123 	/** */
124 	public bool executeFinish(AsyncResultIF result)
125 	{
126 		GError* err = null;
127 
128 		auto p = dzl_directory_reaper_execute_finish(dzlDirectoryReaper, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
129 
130 		if (err !is null)
131 		{
132 			throw new GException( new ErrorG(err) );
133 		}
134 
135 		return p;
136 	}
137 
138 	/**
139 	 * The "remove-file" signal is emitted for each file that is removed by the
140 	 * #DzlDirectoryReaper instance. This may be useful if you want to show the
141 	 * user what was processed by the reaper.
142 	 *
143 	 * Params:
144 	 *     file = a #GFile
145 	 *
146 	 * Since: 3.32
147 	 */
148 	gulong addOnRemoveFile(void delegate(FileIF, DirectoryReaper) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
149 	{
150 		return Signals.connect(this, "remove-file", dlg, connectFlags ^ ConnectFlags.SWAPPED);
151 	}
152 }