1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * gtkD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with gtkD; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
17  */
18 
19 module gtk.SpawnTests;
20 
21 import glib.Spawn;
22 
23 import gio.Application : GioApplication = Application;
24 import gtk.Application;
25 import gtk.ApplicationWindow;
26 import gtk.TextView;
27 import gtk.TextBuffer;
28 import gtk.TextIter;
29 import gtk.Box;
30 import gtk.ScrolledWindow;
31 import gtk.Button;
32 import gtk.Image;
33 
34 import std.string;
35 import std.stdio;
36 
37 class SpawnWindow : ApplicationWindow
38 {
39 	TextView viewInput;
40 	TextView viewOutput;
41 	TextView viewError;
42 	TextIter iterOut;
43 	TextIter iterError;
44 
45 	this(Application application)
46 	{
47 		super(application);
48 		setTitle("Spawn testing");
49 		setupWindow();
50 		setSizeRequest(400,400);
51 		show();
52 	}
53 
54 	private void setupWindow()
55 	{
56 		Box main = new Box(GtkOrientation.VERTICAL, 2);
57 
58 		viewInput = new TextView();
59 		viewOutput = new TextView();
60 		viewError = new TextView();
61 
62 		viewOutput.setValign(GtkAlign.FILL);
63 		viewOutput.setVexpand(true);
64 
65 		main.append(new ScrolledWindow(viewInput));
66 		Button button = new Button("exec");
67 		button.addOnClicked(&execInput);
68 		main.append(button);
69 		main.append(new ScrolledWindow(viewOutput));
70 		main.append(new ScrolledWindow(viewError));
71 
72 		main.setMarginStart(7);
73 		main.setMarginEnd(7);
74 		main.setMarginTop(7);
75 		main.setMarginBottom(7);
76 
77 		setChild(main);
78 	}
79 
80 	private void execInput(Button button)
81 	{
82 		string[] args = std..string.split(viewInput.getBuffer().getText());
83 		exec(args);
84 	}
85 
86 	private bool exec(string[] args)
87 	{
88 		foreach ( size_t i, string arg ; args)
89 		{
90 			writefln("[%s] >%s<", i, arg);
91 		}
92 		Spawn spawn = new Spawn(args[0]);
93 
94 		if (args.length > 1 )
95 		{
96 			for( int i=1 ; i<args.length ; i++ )
97 			{
98 				writefln("SpawnTests.exec adding parameter [%s] %s",i,args[i]);
99 				spawn.addParm(args[i]);
100 			}
101 		}
102 		return exec(spawn);
103 	}
104 
105 	bool childEnded(Spawn spawn)
106 	{
107 		writefln("process %s ended with status %s", viewInput.getBuffer().getText(),spawn.getExitStatus());
108 		return true; //gotta check this.
109 	}
110 
111 	private bool exec(Spawn spawn)
112 	{
113 		//TODO: These functions should accept a string.
114 		viewOutput.getBuffer().setText("");
115 		viewError.getBuffer().setText("");
116 
117 		TextBuffer bufferError = viewError.getBuffer();
118 		TextIter iterError = new TextIter();
119 
120         int result = spawn.commandLineSync(&childEnded, &syncOutput, &syncError);
121 
122 		bufferError.getEndIter(iterError);
123 		viewError.getBuffer().insert(iterError, spawn.getLastError()~"\n");
124 
125 		writefln("exit loop");
126 
127 		spawn.close();
128 		return true;
129 	}
130 
131 	public bool syncOutput(string line)
132 	{
133 		TextIter iter = new TextIter();
134 		viewOutput.getBuffer().getEndIter(iter);
135 		viewOutput.getBuffer().insert(iter, line~"\n");
136 		return true;
137 	}
138 
139 	public bool syncError(string line)
140 	{
141 		TextIter iter = new TextIter();
142 		viewError.getBuffer().getEndIter(iter);
143 		viewError.getBuffer().insert(iter, line~"\n");
144 		return true;
145 	}
146 
147 	public void setInput(string[] args)
148 	{
149 		TextBuffer inBuffer = viewInput.getBuffer();
150 		string t;
151 		foreach ( size_t count, string arg; args)
152 		{
153 			if ( count > 0 ) t ~= " ";
154 			t ~= arg;
155 		}
156 		inBuffer.setText(t);
157 	}
158 
159 	public void setInput(string arg)
160 	{
161 		viewInput.getBuffer().setText(arg);
162 	}
163 }
164 
165 int main(string[] args)
166 {
167 	auto application = new Application("org.gtkd.demo.spawntest", GApplicationFlags.FLAGS_NONE);
168 
169 	application.addOnActivate(delegate void(GioApplication app) { 
170 		SpawnWindow sw = new SpawnWindow(application);
171 
172 		if ( args.length > 1 )
173 		{
174 			sw.setInput(args[1..args.length]);
175 		}
176 		else
177 		{
178 			sw.setInput("/bin/ls");
179 		}
180 	});
181 
182 	return application.run(args);
183 }