summaryrefslogtreecommitdiffstats
path: root/arts/builder/structure.cpp
blob: 832917da2c986b52a345184d5837ca186c19efb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
    /*

    Copyright (C) 1999 Stefan Westerfeld
                       stefan@space.twc.de

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

    */

#include "structure.h"
#include "soundserver.h"
#include "kartsserver.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <kdebug.h>
#include <arts/debug.h>

using namespace std;

ExecutableStructure::ExecutableStructure()
{
}

ExecutableStructure::~ExecutableStructure()
{
	// to make destructor virtual
	// stop execution here?
}

void ExecutableStructure::stopExecute()
{
	arts_debug("TODO: PORT: freeStructure");
}

bool ExecutableStructure::isExecuting()
{
	arts_debug("TODO:PORT:isExecuting()");
	return false;
}

bool ExecutableStructure::saveSession(const char *filename)
{
	arts_debug("TODO:PORT:saveSession()");
#if 0
	assert(execID);

	ArtsCorba::StringSeq_var data;
	arts_debug("saveSession");
	if(Synthesizer->saveSession(execID,true,data))
	{
		arts_debug("ok");
		FILE *file = fopen(filename,"w");
		if(!file) return false;

		unsigned long i;
		for(i=0;i<data->length();i++) fprintf(file,"%s\n",(char *)(*data)[i]);
		fclose(file);

		return true;
	}
	arts_debug("failed");
#endif
	return false;
}

Structure::Structure() :ExecutableStructure()
{
	canvas = 0;
}

void Structure::setCanvas(StructureCanvas *canvas)
{
	this->canvas = canvas;
}

Structure::~Structure()
{
	clear();
	arts_debug("~Structure (releasing structuredesc from structure)");
}

bool Structure::startExecute()
{
#if 0
	assert(!execID);

	arts_debug("PORT: TODO startExecute()");
	// just in case synthesis has been halted before,
	// restart it and hope we'll have enough computing power now
	//Synthesizer->Reset();
	//execID = Synthesizer->createStructure(StructureDesc,preferredservers);
	assert(execID);
#endif

	/* connect to the sound server */
	Arts::SimpleSoundServer server = KArtsServer().server();

	if(server.isNull())
		return false;

	/* move a copy of the structure to the server, so that there will be
	   no latencies in querying what to connect to what */
	vector<string> *savePtr = StructureDesc.saveToList();
	Arts::StructureDesc remoteSD;
	remoteSD = Arts::DynamicCast(server.createObject("Arts::StructureDesc"));
	assert(!remoteSD.isNull());
	remoteSD.loadFromList(*savePtr);
	delete savePtr;

	/* create a structure builder on the server */
	Arts::StructureBuilder builder;
	builder = Arts::DynamicCast(server.createObject("Arts::StructureBuilder"));

	/* create a local factory - this will enable the builder to create gui qt
	   widgets (which need to reside within an qt application to work) */

	Arts::LocalFactory factory;
	builder.addFactory(factory);
	
	/* create the structure on the server */
	structure = Arts::DynamicCast(builder.createObject(remoteSD));

	if (structure.isNull())
		return false;

	structure.start();

	return true;
}

void Structure::stopExecute()
{
	
	// TODO:PORT: verify this code
	structure = Arts::SynthModule::null();
}

void Structure::publish()
{
	arts_debug("PORT: TODO publish()");
	//Synthesizer->publishStructureDesc(StructureDesc);
}

bool Structure::valid()
{
	return StructureDesc.valid();
}

string Structure::name()
{
	return StructureDesc.name();
}

void Structure::rename(const char *newname)
{
	StructureDesc.name(newname);
}

void Structure::addInheritedInterface(const char *iface)
{
	StructureDesc.addInheritedInterface(iface);
}

vector<string> *Structure::inheritedInterfaces()
{
	return StructureDesc.inheritedInterfaces();
}

void Structure::removeInheritedInterface(const char *iface)
{
	StructureDesc.removeInheritedInterface(iface);
}

void Structure::saveInto(FILE *file)
{
	vector<string> *list = StructureDesc.saveToList();

	vector<string>::iterator i;

	for(i = list->begin(); i != list->end(); i++)
		fprintf(file, "%s\n", (*i).c_str());

	delete list;
}

bool Structure::save(const char *filename)
{
	FILE *file = fopen(filename,"w");
	if (!file)
		return false;
	
	saveInto(file);
	
	fclose(file);

	return true;
}

void Structure::clear()
{
	list<StructureComponent *>::iterator ci;

	arts_debug("clc");
/*
	for(ci = ComponentList.begin(); ci != ComponentList.end(); ++ci)
		delete (*ci);

	ComponentList.erase(ComponentList.begin(), ComponentList.end());
	ModuleList.erase(ModuleList.begin(), ModuleList.end());
*/
	for(ci = ComponentList.begin(); ci != ComponentList.end(); ++ci)
		(*ci)->setSelected(true);
	deleteSelected();

	arts_debug("sdc");
	// shouldn't do much, now that we deleted every single component of
	// the structure, but we to it anyway, just to be sure.
	StructureDesc.clear();
}

void Structure::retrieve(const char *pubname)
{
	arts_debug("PORT: TODO: retrieve");
#if 0
	arts_debug("retrieve... %s",pubname);
	ArtsCorba::StructureDesc_var psd = Synthesizer->lookupStructureDesc(pubname);

	arts_debug("psdlookup ok");
	if(psd)
	{
		arts_debug("starting savetolist");
		ArtsCorba::StringSeq_var strseq=psd->saveToList();
		arts_debug("savetolist ok");
		loadFromList(strseq);
		arts_debug("loadfromlist ok");
	}
	arts_debug("retrieve... ok");
#endif
}

void Structure::load(const char *filename)
{
	ifstream infile(filename);
	string line;
	vector<string> strseq;

	while(getline(infile,line))
		strseq.push_back(line);

	loadFromList(strseq);
#if 0
	FILE *infile = fopen(filename,"r");
	ArtsCorba::StringSeq_var strseq = new ArtsCorba::StringSeq;

	char line[1024];
	unsigned long i = 0;

	while(fgets(line,1024,infile))
	{
		// cut eventual CR and/or LFs at the end of the line
		while(strlen(line) && line[strlen(line)-1] < 14)
			line[strlen(line)-1] = 0;

		strseq->length(i+1);
		(*strseq)[i++] = CORBA::string_dup(line);
	}
	fclose(infile);

	arts_debug(">>loadfromlist...");
	loadFromList(strseq);
	arts_debug("<<loadfromlist...");
#endif
}

void Structure::loadFromList(const vector<string>& strseq)
{
	assert(canvas);

	arts_debug(">>clear");
	clear();
	arts_debug("<<clear");

	StructureDesc.loadFromList(strseq);

	vector<Arts::ModuleDesc> *modules = StructureDesc.modules();
	vector<Arts::ModuleDesc>::iterator mi;

	for(mi=modules->begin(); mi != modules->end(); ++mi)
	{
		Module *m = new Module(*mi,StructureDesc,canvas);

		m->show();
		ModuleList.push_back(m);
		ComponentList.push_back(m);
	}
	delete modules;

	vector<Arts::StructurePortDesc> *ports = StructureDesc.ports();
	vector<Arts::StructurePortDesc>::iterator pi;

	for(pi=ports->begin(); pi != ports->end(); ++pi)
	{
		StructurePort *p = new StructurePort(*pi,StructureDesc,canvas);

		p->show();
		ComponentList.push_back(p);
	}
	delete ports;
}

Module *Structure::createModule(const Arts::ModuleInfo& minfo)
{
	assert(canvas);
	Module *m = new Module(minfo,StructureDesc,canvas);

	ComponentList.push_back(m);
	ModuleList.push_back(m);
	return m;
}

StructurePort *Structure::createStructurePort(const Arts::PortType& type)
{	// TODO:PORT: verify this code
#if 0
	arts_debug("TODO:PORT:createStructurePort");
#endif
// portname generation
	unsigned long portindex = 1,l,baselen;;
	char name[100];

	string namebase;
	if(type.direction == Arts::input) {
		// this is a port where our structure puts its results
		// so it is an input port, that is named out
		namebase = "out"; baselen = 3;
    } else {
		// this is a port where our structure gets data from
		// so it is an output port, that is named in
		namebase = "in"; baselen = 2;
	}

	vector<Arts::StructurePortDesc> *sps = StructureDesc.ports();

	for(l=0;l<sps->size();l++) {
		string thisname = (*sps)[l].name();
		if(strncmp(thisname.c_str(), namebase.c_str(), baselen) == 0 &&
           strlen(thisname.c_str()) > baselen)
		{
			unsigned long index2 = atol(&thisname.c_str()[baselen]);
			if(index2 >= portindex) portindex = index2+1;
		}
	}
	delete sps;

	sprintf(name,"%s%ld",namebase.c_str(),portindex);
	arts_debug("new Portname: %s",name);
	Arts::StructurePortDesc spd =
		StructureDesc.createStructurePortDesc(type,name);

	assert(canvas);
	StructurePort *s = new StructurePort(spd,StructureDesc,canvas);
	ComponentList.push_back(s);
	return s;
}

list<Module *> *Structure::getModuleList()
{
	return(&ModuleList);
}

list<StructureComponent *> *Structure::getComponentList()
{
	return(&ComponentList);
}

long Structure::countSelected()
{
	list<StructureComponent *>::iterator ci;
	long targets = 0;

	for(ci=ComponentList.begin();ci!=ComponentList.end();++ci)
		if((*ci)->selected()) targets++;

	return targets;
}

void Structure::deleteSelected()
{
	arts_debug("deleteSelected...");

	// remove module from the ModuleList
	list<Module *>::iterator mi;
	for(mi=ModuleList.begin();mi!=ModuleList.end();)
	{
		if((*mi)->selected())
		{
			mi = ModuleList.erase(mi);
		}
		else mi++;
	}

	// disconnect ports (it might be useful to get this right in the model
	// instead of doing it in the view - however at it works without end
	// user visible bugs like this)
	
	list<StructureComponent *>::iterator ci;
	list<ModulePort *> allPorts;

	for(ci = ComponentList.begin(); ci!=ComponentList.end(); ++ci)
		if((*ci)->selected())
			(*ci)->dumpPorts(allPorts);

	list<ModulePort *>::iterator pi;
	for(pi = allPorts.begin(); pi != allPorts.end(); ++pi)
		(*pi)->PortDesc.disconnectAll();

	// and from the ComponentList (the future ;)

	ci = ComponentList.begin();
	while(ci!=ComponentList.end())
	{
		if((*ci)->selected())
		{
			delete (*ci);
			ci = ComponentList.erase(ci);
		}
		else ci++;
	}

	arts_debug("deleteSelected ok.");
}

StructureComponent *Structure::componentAt(long x, long y, bool ignore_selected)
{
	list<StructureComponent *>::iterator ci;

	for(ci=ComponentList.begin();ci!=ComponentList.end();++ci)
	{
		StructureComponent *c = *ci;

		if(x >= c->x() && x < c->x()+c->width() &&
           y >= c->y() && y < c->y()+c->height())
		{
			if((c->selected() && !ignore_selected) || !c->selected()) return c;
		}
	}

	return 0;
}