summaryrefslogtreecommitdiffstats
path: root/parts/appwizard/appwizarddlg.cpp
blob: 176bf9ccee910ef55e6332d7027fe666dfb819d9 (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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
/***************************************************************************
 *   Copyright (C) 2001-2002 by Bernd Gehrmann                             *
 *   bernd@tdevelop.org                                                    *
 *   Copyright (C) 2001 by Sandy Meier                                     *
 *   smeier@tdevelop.org                                                   *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "appwizarddlg.h"

#include <tqvbox.h>
#include <tqbuttongroup.h>
#include <tqcombobox.h>
#include <tqtabwidget.h>
#include <tqwidgetstack.h>
#include <tqdir.h>
#include <tqfileinfo.h>
#include <tqgrid.h>
#include <tqheader.h>
#include <tqmap.h>
#include <tqmultilineedit.h>
#include <tqpushbutton.h>
#include <tqradiobutton.h>
#include <tqregexp.h>
#include <tqtextstream.h>
#include <tqtextview.h>
#include <tqtoolbutton.h>
#include <tqtooltip.h>
#include <tqvalidator.h>
#include <tqtimer.h>
#include <klistview.h>
#include <kiconview.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kglobalsettings.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kprocess.h>
#include <kstandarddirs.h>
#include <ktempfile.h>
#include <kiconloader.h>
#include <tdefiledialog.h>
#include <tdefile.h>
#include <kapplication.h>
#include <kpopupmenu.h>

#include <ktrader.h>
#include <tdeparts/componentfactory.h>
#include <tdeio/netaccess.h>
#include <tqfile.h>
#include <kmacroexpander.h>
#include <karchive.h>
#include <ktar.h>
#include <ktempdir.h>
#include <tdefileitem.h>
#include <tdeio/chmodjob.h>

#include <tqlayout.h>

#include "domutil.h"
#include "kdevversioncontrol.h"
#include "kdevmakefrontend.h"
#include "kdevpartcontroller.h"
#include "kdevplugincontroller.h"
#include "kdevappfrontend.h"
#include "kdevplugininfo.h"
#include "kdevlicense.h"
#include "kdevcore.h"
#include "appwizardfactory.h"
#include "appwizardpart.h"
#include "filepropspage.h"
#include "misc.h"
#include "profilesupport.h"
#include "filetemplate.h"
#include "settings.h"

#include "blockingkprocess.h"

#include "profileengine.h"
#include "profile.h"

#include "propeditor/property.h"
#include "propeditor/multiproperty.h"
#include "propeditor/propertylist.h"
#include "propeditor/propertyeditor.h"

AppWizardDialog::AppWizardDialog(AppWizardPart *part, TQWidget *parent, const char *name)
    : AppWizardDialogBase(parent, name,true), m_pCurrentAppInfo(0),
	m_profileSupport(new ProfileSupport(part))
{
	kdDebug( 9010 ) << "  ** AppWizardDialog::AppWizardDialog()" << endl;

	m_customOptions = 0L;
	loadLicenses();
    connect( this, TQT_SIGNAL( selected( const TQString & ) ), this, TQT_SLOT( pageChanged() ) );

	helpButton()->hide();
    templates_listview->header()->hide();
	templates_listview->setColumnWidthMode(0, TQListView::Maximum);	//to provide horiz scrollbar.

	m_templatesMenu = new KPopupMenu(templates_listview);
	m_templatesMenu->insertItem(i18n("&Add to Favorites"), this, TQT_SLOT(addTemplateToFavourites()));

	m_favouritesMenu = new KPopupMenu(favourites_iconview);
	m_favouritesMenu->insertItem(i18n("&Remove Favorite"), this, TQT_SLOT(removeFavourite()));

    m_pathIsValid=false;
    m_part = part;
    m_projectLocationWasChanged=false;
    m_appsInfo.setAutoDelete(true);
    m_tempFiles.setAutoDelete(true);

    TDEConfig *config = kapp->config();

	//config->setGroup("AppWizard");
	//templates_tabwidget->setCurrentPage(config->readNumEntry("CurrentTab", 0));

	config->setGroup("General Options");
    TQString defaultProjectsDir = config->readPathEntry("DefaultProjectsDir", TQDir::homeDirPath()+"/");

    KStandardDirs *dirs = AppWizardFactory::instance()->dirs();
    TQStringList m_templateNames = dirs->findAllResources("apptemplates", TQString(), false, true);

    kdDebug(9010) << "Templates: " << endl;
    TQStringList categories;

    TQStringList::Iterator it;
    for (it = m_templateNames.begin(); it != m_templateNames.end(); ++it) {
        kdDebug(9010) << (*it) << endl;

        ApplicationInfo *info = new ApplicationInfo;
		info->propValues = new PropertyLib::PropertyList();
		info->templateFile = TDEGlobal::dirs()->findResource("apptemplates", *it);
        info->templateName = (*it);

		TDEConfig templateConfig(info->templateFile);
        templateConfig.setGroup("General");

        info->name = templateConfig.readEntry("Name");
        info->icon = templateConfig.readEntry("Icon");
        info->comment = templateConfig.readEntry("Comment");
        info->fileTemplates = templateConfig.readEntry("FileTemplates");
        info->openFilesAfterGeneration = templateConfig.readListEntry("ShowFilesAfterGeneration");
        TQString destDir = templateConfig.readPathEntry("DefaultDestinatonDir", defaultProjectsDir);
        destDir.replace(TQRegExp("HOMEDIR"), TQDir::homeDirPath());
        info->defaultDestDir = destDir;
        TQString category = templateConfig.readEntry("Category");
        // format category to a unique status
        if (category.right(1) == "/")
            category.remove(category.length()-1, 1); // remove /
        if (category.left(1) != "/")
            category.prepend("/"); // prepend /
        categories.append(category);
        info->category = category;
		info->sourceArchive = templateConfig.readEntry("Archive");

		// Grab includes list
		TQStringList groups = templateConfig.groupList();
		groups.remove("General");
		TQStringList::Iterator group = groups.begin();
		for(  ; group != groups.end(); ++group)
		{
			templateConfig.setGroup( (*group) );
			TQString type = templateConfig.readEntry("Type").lower();
			if( type == "include" )  // Add value
			{
				info->includes.append( templateConfig.readEntry( "File" ) );
				kdDebug(9010) << "Adding: " << templateConfig.readEntry( "File" ) << endl;
			}
		}

		// Build builtins map to bootstrap.
		TQString source = kdevRoot( info->templateName );
		info->subMap.insert("tdevelop", source );

		// Add includes to the main template...
		TQStringList::Iterator include = info->includes.begin();
		for( ; include != info->includes.end(); ++include)
		{
			if( !(*include).isEmpty() )
			{
				TQString file = KMacroExpander::expandMacros( ( *include ), info->subMap);
				TDEConfig tmpCfg( file );
				tmpCfg.copyTo( "", &templateConfig);
				kdDebug(9010) << "Merging: " << tmpCfg.name() << endl;
			}
		}

		groups = templateConfig.groupList();  // Must get this again since its changed!
		group = groups.begin();
		for(  ; group != groups.end(); ++group)
		{
			templateConfig.setGroup( (*group) );
			TQString type = templateConfig.readEntry("Type", "value").lower();
			kdDebug(9010) << "Reading " <<  (*group) << " of type " << type << endl;
			if( type == "value" )  // Add value
			{
				TQString name = templateConfig.readEntry( "Value" );
				TQString label = templateConfig.readEntry( "Comment" );
				TQString type = templateConfig.readEntry( "ValueType", "String" );
				TQVariant::Type variantType = TQVariant::nameToType( type.latin1());
				TQVariant value = templateConfig.readPropertyEntry( "Default", variantType );
				value.cast( variantType );  // fix this in tdelibs...
				if( !name.isEmpty() && !label.isEmpty() )
					info->propValues->addProperty( new PropertyLib::Property( (int)variantType, name, label, value ) );

			}
			else if( type == "install" ) // copy dir
			{
				installFile file;
				file.source = templateConfig.readPathEntry("Source");
				file.dest = templateConfig.readPathEntry("Dest");
				file.process = templateConfig.readBoolEntry("Process",true);
				file.isXML = templateConfig.readBoolEntry("EscapeXML",false);
				file.option = templateConfig.readEntry("Option");
				info->fileList.append(file);
			}
			else if( type == "install archive" )
			{
				installArchive arch;
				arch.source = templateConfig.readPathEntry("Source");
				arch.dest = templateConfig.readPathEntry("Dest");
				arch.process = templateConfig.readBoolEntry("Process",true);
				arch.option = templateConfig.readEntry("Option", "" );
				info->archList.append(arch);
			}
			else if( type == "mkdir" )
			{
				installDir dir;
				dir.dir = templateConfig.readPathEntry("Dir");
				dir.option = templateConfig.readEntry("Option", "" );
				dir.perms = templateConfig.readNumEntry("Perms", 0777 );
				info->dirList.append(dir);
			}
			else if( type == "finishcmd" )
			{
                                info->finishCmd=templateConfig.readPathEntry("Command");
                                info->finishCmdDir=templateConfig.readPathEntry("Directory");
			}
			else if( type == "ui")
			{
				TQString name = templateConfig.readPathEntry("File");
				info->customUI = name;
			}
			else if( type == "message" )
			{
				info->message = templateConfig.readEntry( "Comment" );
			}
		}


        m_appsInfo.append(info);
    }

    // Insert categories into list view
    categories.sort();
    for (it = categories.begin(); it != categories.end(); ++it)
        insertCategoryIntoTreeView(*it);

    // Insert items into list view
    TQPtrListIterator<ApplicationInfo> ait(m_appsInfo);
    for (; ait.current(); ++ait) {
        TQListViewItem *item = m_categoryMap.find(ait.current()->category);
        if (item)
		{
            item = new KListViewItem(item, ait.current()->name);
			item->setPixmap(0, SmallIcon("tdevelop"));
		}
        else
            kdDebug(9010) << "Error can't find category in categoryMap: "
                          << ait.current()->category << endl;
        ait.current()->item = item;
    }

	//Load favourites from config
	populateFavourites();

	TQString author, email;
    AppWizardUtil::guessAuthorAndEmail(&author, &email);
    author_edit->setText(author);
    email_edit->setText(email);
    TQToolTip::add( dest_edit->button(), i18n("Choose projects directory") );
    dest_edit->setURL(defaultProjectsDir);
    dest_edit->setMode(KFile::Directory|KFile::ExistingOnly|KFile::LocalOnly);

    loadVcs();

    //    addPage(m_sdi_fileprops_page,"Class/File Properties");

    //    licenseChanged();

    setNextEnabled(generalPage, false);

//    TQRegExp appname_regexp ("[a-zA-Z][a-zA-Z0-9_]*"); //Non-Unicode version
    /* appname will start with a letter, and will contain letters,
       digits or underscores. */
    TQRegExp appname_regexp ("[a-zA-Z][a-zA-Z0-9_]*");
    // How about names like "__" or "123" for project name? Are they legal?
    TQRegExpValidator *appname_edit_validator;
    appname_edit_validator = new TQRegExpValidator (appname_regexp,
                                                   TQT_TQOBJECT(appname_edit),
                                                   "AppNameValidator");
    appname_edit->setValidator(appname_edit_validator);

    // insert the licenses into the license_combo
    TQDict< KDevLicense > lics( licenses() );
    TQDictIterator< KDevLicense > dit(lics);
    int idx=1;
    for( ; dit.current(); ++dit )
    {
        license_combo->insertItem( dit.currentKey(), idx++ );
        if( dit.currentKey() == "GPL" )
            license_combo->setCurrentItem( idx - 1 );
    }

	connect( license_combo, TQT_SIGNAL(activated(int)), this, TQT_SLOT(licenseChanged()) );

	m_custom_options_layout = new TQHBoxLayout( custom_options );
	m_custom_options_layout->setAutoAdd(true);

	showTemplates(false);
}

AppWizardDialog::~AppWizardDialog()
{}

void AppWizardDialog::loadVcs()
{
	m_vcsForm = new VcsForm();

	int i=0;
	m_vcsForm->combo->insertItem( i18n("no version control system", "None"), i );
	m_vcsForm->stack->addWidget( 0, i++ );

	// We query for all vcs integrators for KDevelop
	KTrader::OfferList offers = KTrader::self()->query("TDevelop/VCSIntegrator", "");
	KTrader::OfferList::const_iterator serviceIt = offers.begin();
	for (; serviceIt != offers.end(); ++serviceIt)
	{
		KService::Ptr service = *serviceIt;
		kdDebug(9010) << "AppWizardDialog::loadVcs: creating vcs integrator "
			<< service->name() << endl;

		KLibFactory *factory = KLibLoader::self()->factory(TQFile::encodeName(service->library()));
		if (!factory) {
			TQString errorMessage = KLibLoader::self()->lastErrorMessage();
			kdDebug(9010) << "There was an error loading the module " << service->name() << endl <<
			"The diagnostics is:" << endl << errorMessage << endl;
			continue;
		}
		TQStringList args;
		TQObject *obj = factory->create(0, service->name().latin1(),
									"KDevVCSIntegrator", args);
		KDevVCSIntegrator *integrator = (KDevVCSIntegrator*) obj;

		if (!integrator)
			kdDebug(9010) << "    failed to create vcs integrator " << service->name() << endl;
		else
		{
			kdDebug(9010) << "    success" << endl;

			TQString vcsName = service->property("X-TDevelop-VCS").toString();
			m_vcsForm->combo->insertItem(vcsName, i);
			m_integrators.insert(vcsName, integrator);

			VCSDialog *vcs = integrator->integrator(m_vcsForm->stack);
			if (vcs)
			{
				m_integratorDialogs[i] = vcs;
				TQWidget *w = vcs->self();
				if (w)
					m_vcsForm->stack->addWidget(w, i++);
				else
					kdDebug(9010) << "    integrator widget is 0" << endl;
			}
			else
				kdDebug(9010) << "    integrator is 0" << endl;
		}
	}

	addPage(m_vcsForm, i18n("Version Control System"));
}

void AppWizardDialog::updateNextButtons()
{
	bool validGeneralPage = m_pCurrentAppInfo
							&& !appname_edit->text().isEmpty()
							&& m_pathIsValid;
	bool validPropsPage = !version_edit->text().isEmpty()
							&& !author_edit->text().isEmpty();

	setFinishEnabled(m_lastPage, validGeneralPage && validPropsPage);
	nextButton()->setEnabled(
		currentPage() == generalPage ? validGeneralPage : validPropsPage );
}

void AppWizardDialog::textChanged()
{
//    licenseChanged();

    updateNextButtons();
}

void AppWizardDialog::licenseChanged()
{
	TQValueList<AppWizardFileTemplate>::Iterator it;
	if( license_combo->currentItem() == 0 )
	{
		for (it = m_fileTemplates.begin(); it != m_fileTemplates.end(); ++it)
		{
			TQMultiLineEdit *edit = (*it).edit;
			edit->setText( TQString() );
		}
	} else {
		KDevLicense* lic = licenses()[ license_combo->currentText() ];
		for (it = m_fileTemplates.begin(); it != m_fileTemplates.end(); ++it) {
			TQString style = (*it).style;
			TQMultiLineEdit *edit = (*it).edit;

			KDevFile::CommentingStyle commentStyle = KDevFile::CPPStyle;
			if (style == "PStyle") {
				commentStyle = KDevFile::PascalStyle;
			} else if (style == "AdaStyle") {
				commentStyle = KDevFile::AdaStyle;
			} else if (style == "ShellStyle") {
				commentStyle = KDevFile::BashStyle;
			} else if (style == "XMLStyle") {
				commentStyle = KDevFile::XMLStyle;
			}

			TQString text;
			text = lic->assemble( commentStyle, author_edit->text(), email_edit->text() , 0 );
			edit->setText(text);
		}
	}
}

TQString AppWizardDialog::kdevRoot(const TQString &templateName ) const
{
	TQString source;
    TQFileInfo finfo(templateName);
    TQDir dir(finfo.dir());
    dir.cdUp();
    return dir.absPath();
}

void AppWizardDialog::accept()
{
    TQFileInfo fi(finalLoc_label->text());
    // check /again/ whether the dir already exists; maybe users create it in the meantime
    if (fi.exists() ) {
        KMessageBox::sorry(this, i18n("The directory you have chosen as the location for "
                                      "the project already exists."));
        showPage(generalPage);
        appname_edit->setFocus();
        projectLocationChanged();
        return;
    }

    if( !TQFileInfo(dest_edit->url()).isWritable()  ){
        KMessageBox::sorry(this, i18n("The directory you have chosen as the location for "
                                      "the project is not writeable."));
        showPage(generalPage);
        appname_edit->setFocus();
        projectLocationChanged();
        return;
    }

	TQString source = kdevRoot( m_pCurrentAppInfo->templateName );

	// Unpack template archive to temp dir, and get the name
	kdDebug(9010) << "Unpacking archive to temp dir" << endl;
	KTempDir archDir;
	archDir.setAutoDelete(true);
	KTar templateArchive( source + "/" + m_pCurrentAppInfo->sourceArchive, "application/x-gzip" );
	if( templateArchive.open( IO_ReadOnly ) )
	{
		//templateArchive.directory()->copyTo(archDir.name(), true);
		unpackArchive(templateArchive.directory(), archDir.name(), false);
	}
	else
	{
		KMessageBox::sorry(this, i18n("The template %1 cannot be opened.").arg( source + "/" + m_pCurrentAppInfo->sourceArchive ) );
		templateArchive.close();
		return;
	}
	templateArchive.close();

	kdDebug(9010) << "build macro map" << endl;
	// Build KMacroExpander map
	//m_customOptions->dataForm()->fillPropertyMap(&m_pCurrentAppInfo->subMap);
	PropertyLib::PropertyList::Iterator idx = m_pCurrentAppInfo->propValues->begin();
	for( ; idx != m_pCurrentAppInfo->propValues->end(); ++idx)
		m_pCurrentAppInfo->subMap.insert( idx.data()->name(), idx.data()->value().toString() );

	m_pCurrentAppInfo->subMap.insert("src", archDir.name() );
	m_pCurrentAppInfo->subMap.insert("dest", finalLoc_label->text() );
	m_pCurrentAppInfo->subMap.insert("APPNAME", appname_edit->text() );
	m_pCurrentAppInfo->subMap.insert("APPNAMELC", appname_edit->text().lower() );
	m_pCurrentAppInfo->subMap.insert("APPNAMESC", TQString(appname_edit->text()[0]).upper() + appname_edit->text().mid(1));
	m_pCurrentAppInfo->subMap.insert("APPNAMEUC", appname_edit->text().upper() );
	m_pCurrentAppInfo->subMap.insert("AUTHOR", author_edit->text() );
	m_pCurrentAppInfo->subMap.insert("EMAIL", email_edit->text() );
	m_pCurrentAppInfo->subMap.insert("VERSION", version_edit->text());
	m_pCurrentAppInfo->subMap.insert( "I18N", "i18n" );
	m_pCurrentAppInfo->subMap.insert("YEAR", TQString::number( TQDate::currentDate().year() ) );

	// This isn't too pretty, but we have several templates that use TDEAboutData::License_${LICENSE}
	// and unsurprisingly, TDEAboutData doesn't cover every imaginable case.
	// These are the licenses known to KDE-3.2 TDEAboutData, KDevelop doesn't have all of these as prepared options today
	TQString license = license_combo->currentText();
	if ( license == "GPL" || license == "GPL_V2" || license == "LGPL" || license == "LGPL_V2"||
		license == "BSD" || license == "NCSA" || license == "MIT" || license == "Artistic" || 
		license == "QPL" || license == "TQPL_V1_0" )
	{
		m_pCurrentAppInfo->subMap.insert("LICENSE", license );
	}
	else
	{
		m_pCurrentAppInfo->subMap.insert("LICENSE", "Custom" );
	}


	TQStringList cleanUpSubstMap;
	cleanUpSubstMap << "src" << "I18N" << "tdevelop";


	kdDebug(9010) << "add template files" << endl;
	// Add template files to the fileList
	installDir templateDir;
	templateDir.dir = "%{dest}/templates";
	m_pCurrentAppInfo->dirList.prepend(templateDir);

	installDir baseDir;
	baseDir.dir = "%{dest}";
	m_pCurrentAppInfo->dirList.prepend( baseDir );

	// This is too silly for words, but it's either this or reimplementing FileTemplate
	TQString tempProjectDomSource = "<!DOCTYPE tdevelop><tdevelop><general><author>%1</author><email>%2</email><version>%3</version></general></tdevelop>";
	tempProjectDomSource = tempProjectDomSource.arg( author_edit->text() ).arg( email_edit->text() ).arg( version_edit->text() );
	TQDomDocument tempProjectDom;
	tempProjectDom.setContent( tempProjectDomSource );

    TQValueList<AppWizardFileTemplate>::Iterator it;
    for (it = m_fileTemplates.begin(); it != m_fileTemplates.end(); ++it) {
        KTempFile *tempFile = new KTempFile();
        m_tempFiles.append(tempFile);

		TQString templateText( FileTemplate::makeSubstitutions( tempProjectDom, (*it).edit->text() ) );
		TQFile f;
		f.open(IO_WriteOnly, tempFile->handle());
		TQTextStream temps(&f);
		temps.setEncoding(TQTextStream::UnicodeUTF8);
		temps << templateText;
		f.flush();
		TQString templateName( TQString( "%1_TEMPLATE" ).arg( (*it).suffix ).upper() );
		cleanUpSubstMap << templateName;
		m_pCurrentAppInfo->subMap.insert( templateName, KMacroExpander::expandMacros(templateText , m_pCurrentAppInfo->subMap)  );

		installFile file;
		file.source = tempFile->name();
		file.dest = TQString( "%{dest}/templates/%1" ).arg( (*it).suffix );
		file.process = true;
		file.isXML = false;
		m_pCurrentAppInfo->fileList.append( file );
    }

	// Add license file to the file list
	TQString licenseFile, licenseName = i18n("Custom");

    if( license_combo->currentItem() != 0 )
    {
        licenseName = license_combo->currentText();
        KDevLicense* lic = licenses()[ licenseName ];
        if( lic )
        {
            TQStringList files( lic->copyFiles() );
			TQStringList::Iterator it = files.begin();
			for( ; it != files.end(); ++it )
			{
				installFile file;
				file.source = TQString( "%{tdevelop}/template-common/%1" ).arg( *it );
				file.dest = TQString("%{dest}/%1").arg( *it );
				file.process = true;
				file.isXML = false;
				m_pCurrentAppInfo->fileList.append( file );
			}

			m_pCurrentAppInfo->subMap.insert("LICENSEFILE", files.first()  );
        }
    }

	// Run macro expander on both the dir map and file maps
	TQValueList<installFile>::Iterator fileIt = m_pCurrentAppInfo->fileList.begin();
	for( ; fileIt != m_pCurrentAppInfo->fileList.end(); ++fileIt)
	{
		(*fileIt).source = KMacroExpander::expandMacros((*fileIt).source , m_pCurrentAppInfo->subMap);
		kdDebug(9010) << "Updating file dest: " << (*fileIt).dest << " with " << KMacroExpander::expandMacros((*fileIt).dest , m_pCurrentAppInfo->subMap) << endl;
		(*fileIt).dest = KMacroExpander::expandMacros((*fileIt).dest , m_pCurrentAppInfo->subMap);
	}

	TQValueList<installArchive>::Iterator archIt = m_pCurrentAppInfo->archList.begin();
	for( ; archIt != m_pCurrentAppInfo->archList.end(); ++archIt)
	{
		(*archIt).source = KMacroExpander::expandMacros((*archIt).source , m_pCurrentAppInfo->subMap);
		(*archIt).dest = KMacroExpander::expandMacros((*archIt).dest , m_pCurrentAppInfo->subMap);
	}

	TQValueList<installDir>::Iterator dirIt = m_pCurrentAppInfo->dirList.begin();
	for( ; dirIt != m_pCurrentAppInfo->dirList.end(); ++dirIt)
	{
		(*dirIt).dir = KMacroExpander::expandMacros((*dirIt).dir , m_pCurrentAppInfo->subMap);
	}

    if( !m_pCurrentAppInfo->finishCmd.isEmpty() )
    {
        m_pCurrentAppInfo->finishCmd = KMacroExpander::expandMacros(
                m_pCurrentAppInfo->finishCmd, m_pCurrentAppInfo->subMap );
        m_pCurrentAppInfo->finishCmdDir = KMacroExpander::expandMacros(
                m_pCurrentAppInfo->finishCmdDir, m_pCurrentAppInfo->subMap );
    }

	TQMap<TQString,TQString>::Iterator mapIt( m_pCurrentAppInfo->subMap.begin() );
	for( ; mapIt != m_pCurrentAppInfo->subMap.end(); ++mapIt )
	{
		TQString escaped( mapIt.data() );
		escaped.replace( "&", "&amp;" );
		escaped.replace( "<", "&lt;" );
		escaped.replace( ">", "&gt;" );
		m_pCurrentAppInfo->subMapXML.insert( mapIt.key(), escaped );
	}

	// Create dirs
	dirIt = m_pCurrentAppInfo->dirList.begin();
	for( ; dirIt != m_pCurrentAppInfo->dirList.end(); ++dirIt)
	{
		kdDebug( 9010 ) << "Process dir " << (*dirIt).dir  << endl;
		if( m_pCurrentAppInfo->subMap[(*dirIt).option] != "false" )
		{
			if( ! TDEIO::NetAccess::mkdir( (*dirIt).dir, this ) )
			{
				KMessageBox::sorry(this, i18n("The directory %1 cannot be created.").arg( (*dirIt).dir ) );
				return;
			}
		}
	}
	// Unpack archives
	archIt = m_pCurrentAppInfo->archList.begin();
	for( ; archIt != m_pCurrentAppInfo->archList.end(); ++archIt)
	{
		if( m_pCurrentAppInfo->subMap[(*archIt).option] != "false" )
		{
			kdDebug( 9010 ) << "unpacking archive " << (*archIt).source << endl;
			KTar archive( (*archIt).source, "application/x-gzip" );
			if( archive.open( IO_ReadOnly ) )
			{
				unpackArchive( archive.directory(), (*archIt).dest, (*archIt).process );
			}
			else
			{
				KMessageBox::sorry(this, i18n("The archive %1 cannot be opened.").arg( (*archIt).source ) );
				archive.close();
				return;
			}
			archive.close();
		}

	}

	// Copy files & Process
	fileIt = m_pCurrentAppInfo->fileList.begin();
	for( ; fileIt != m_pCurrentAppInfo->fileList.end(); ++fileIt)
	{
		kdDebug( 9010 ) << "Process file " << (*fileIt).source << endl;
		if( m_pCurrentAppInfo->subMap[(*fileIt).option] != "false" )
		{
			if( !copyFile( *fileIt ) )
			{
				KMessageBox::sorry(this, i18n("The file %1 cannot be created.").arg( (*fileIt).dest) );
				return;
			}
			setPermissions(*fileIt);
		}
	}
    // if dir still does not exist
    if (!fi.dir().exists()) {
      KMessageBox::sorry(this, i18n("The directory above the chosen location does not exist and cannot be created."));
      showPage(generalPage);
      dest_edit->setFocus();
      return;
    }

//	KMessageBox::information(this, KMacroExpander::expandMacros(m_pCurrentAppInfo->message, m_pCurrentAppInfo->subMap));

	TQStringList::Iterator cleanIt = cleanUpSubstMap.begin();
	for(;cleanIt != cleanUpSubstMap.end(); ++cleanIt )
	{
		m_pCurrentAppInfo->subMap.remove( *cleanIt );
	}

      if  (!m_pCurrentAppInfo->finishCmd.isEmpty())
      {
        BlockingTDEProcess proc;
        proc.setWorkingDirectory( m_pCurrentAppInfo->finishCmdDir );
        proc.setUseShell( true );
        proc << "cd" << m_pCurrentAppInfo->finishCmdDir << "&&";
        proc << m_pCurrentAppInfo->finishCmd;
        kdDebug(9010) << "Executing:" << proc.args() << endl;
        proc.start( TDEProcess::NotifyOnExit );
        if( !proc.isRunning() && !proc.normalExit() )
        {
            kdDebug(9010) << "Couldn't execute: " << proc.args() << endl;
        }
      }



	int id = m_vcsForm->stack->id(m_vcsForm->stack->visibleWidget());
	if (id)
	{
		VCSDialog *vcs = m_integratorDialogs[id];
		if (vcs)
		{
			kdDebug(9010) << "vcs integrator dialog is ready" << endl;
			vcs->accept();
		}
		else
			kdDebug(9010) << "no vcs integrator dialog" << endl;
	}
	else
		kdDebug(9010) << "vcs integrator wasn't selected" << endl;

	openAfterGeneration();
	TQWizard::accept();
}

bool AppWizardDialog::copyFile( const installFile& file )
{
	kdDebug(9010) << "Copying file" << file.dest << endl;
	return
		copyFile( file.source, file.dest, file.isXML, file.process );
}

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

bool AppWizardDialog::copyFile( const TQString &source, const TQString &dest, bool isXML, bool process )
{
	kdDebug( 9010 ) << "Copy: " << source << " to " << dest << endl;
	if( process )
	{
		// Process the file and save it at the destFile location
		TQFile inputFile( source);
		TQFile outputFile( dest );

		const TQMap<TQString,TQString> &subMap = isXML ?
			m_pCurrentAppInfo->subMapXML : m_pCurrentAppInfo->subMap;
		if( inputFile.open( IO_ReadOnly ) && outputFile.open(IO_WriteOnly) )
		{
			TQTextStream input( &inputFile );
			input.setEncoding(TQTextStream::UnicodeUTF8);
			TQTextStream output( &outputFile );
			output.setEncoding(TQTextStream::UnicodeUTF8);
			while( !input.atEnd() )
				output << KMacroExpander::expandMacros(input.readLine(), subMap) << "\n";
			// Preserve file mode...
			struct stat fmode;
			::fstat( inputFile.handle(), &fmode);
			::fchmod( outputFile.handle(), fmode.st_mode );

		}
		else
		{
			inputFile.close();
			outputFile.close();
			return false;
		}
	}
	else
	{
		// Copy the source file to the destFile.
		return TDEIO::NetAccess::copy( source, dest, this );
	}
	return true;
}

void AppWizardDialog::unpackArchive( const KArchiveDirectory *dir, const TQString &dest, bool process )
{
	TDEIO::NetAccess::mkdir( dest , this );
	kdDebug(9010) << "Dir : " << dir->name() << " at " << dest << endl;
	TQStringList entries = dir->entries();
	kdDebug(9010) << "Entries : " << entries.join(",") << endl;

	KTempDir tdir;

	TQStringList::Iterator entry = entries.begin();
	for( ; entry != entries.end(); ++entry )
	{

		if( dir->entry( (*entry) )->isDirectory()  )
		{
			const KArchiveDirectory *file = (KArchiveDirectory *)dir->entry( (*entry) );
			unpackArchive( file , dest + "/" + file->name(), process);
		}
		else if( dir->entry( (*entry) )->isFile()  )
		{
			const KArchiveFile *file = (KArchiveFile *) dir->entry( (*entry) );
			if( !process )
			{
				file->copyTo( dest );
				setPermissions(file, dest + "/" + file->name());
			}
			else
			{
				file->copyTo(tdir.name());
				// assume that an archive does not contain XML files
				// ( where should we currently get that info from? )
				if ( !copyFile( TQDir::cleanDirPath(tdir.name()+"/"+file->name()), dest + "/" + file->name(), false, process ) )
				{
					KMessageBox::sorry(this, i18n("The file %1 cannot be created.").arg( dest) );
					return;
				}
				setPermissions(file, dest + "/" + file->name());
			}
		}
	}
	tdir.unlink();
}

void AppWizardDialog::templatesTreeViewClicked(TQListViewItem *item)
{
	if( m_customOptions )
		delete m_customOptions;

    // Delete old file template pages
    while (!m_fileTemplates.isEmpty()) {
        TQMultiLineEdit *edit = m_fileTemplates.first().edit;
        removePage(edit);
        delete edit;
        m_fileTemplates.remove(m_fileTemplates.begin());
    }
    m_lastPage = 0;

    ApplicationInfo *info = templateForItem(item);
    if (info) {
        m_pCurrentAppInfo = info;
        if (!info->icon.isEmpty()) {
            TQFileInfo fi(info->templateName);
            TQDir dir(fi.dir());
            dir.cdUp();
            TQPixmap pm;
            pm.load(dir.filePath(info->icon));
            icon_label->setPixmap(pm);
        } else {
            icon_label->clear();
        }
        desc_textview->setText(info->comment);
//        dest_edit->setURL(info->defaultDestDir);
        m_projectLocationWasChanged = false;
        //projectNameChanged(); // set the dest new

		// Populate new custom options form
		m_customOptions = new PropertyLib::PropertyEditor( custom_options );
		m_customOptions->populateProperties(info->propValues);


        // Create new file template pages
        TQStringList l = TQStringList::split(",", info->fileTemplates);
        if (l.empty()) //if the app template doesn't show file templates, we need to set another m_lastPage, aleXXX
           m_lastPage=m_vcsForm;

        TQStringList::ConstIterator it = l.begin();
        while (it != l.end()) {
            AppWizardFileTemplate fileTemplate;
            fileTemplate.suffix = *it;
            ++it;
            if (it != l.end()) {
                fileTemplate.style = *it;
                ++it;
            } else
                fileTemplate.style = "";

            TQMultiLineEdit *edit = new TQMultiLineEdit(this);
            edit->setWordWrap(TQTextEdit::NoWrap);
            edit->setFont(TDEGlobalSettings::fixedFont());
            if (it == l.end())
                m_lastPage = edit;
            fileTemplate.edit = edit;
            addPage(edit, i18n("Template for .%1 Files").arg(fileTemplate.suffix));
            m_fileTemplates.append(fileTemplate);
        }
        licenseChanged();	// to populate the template views
        textChanged(); // update Next button state
    } else {
	m_customOptions=0;
        m_pCurrentAppInfo=0;
        icon_label->clear();
        desc_textview->clear();
        nextButton()->setEnabled(false);
    }
}


void AppWizardDialog::destButtonClicked(const TQString& dir)
{
    if(!dir.isEmpty()) {

        // set new location as default project dir?
        TDEConfig *config = kapp->config();
        config->setGroup("General Options");
        TQDir defPrjDir( config->readPathEntry("DefaultProjectsDir", TQDir::homeDirPath()) );
        TQDir newDir (dir);
        kdDebug(9010) << "DevPrjDir == newdir?: " << defPrjDir.absPath() << " == " << newDir.absPath() << endl;
        if (defPrjDir != newDir) {
            if (KMessageBox::questionYesNo(this, i18n("Set default project location to: %1?").arg( newDir.absPath() ),
                                           i18n("New Project"), i18n("Set"), i18n("Do Not Set")) == KMessageBox::Yes)
            {
                config->writePathEntry("DefaultProjectsDir", newDir.absPath() + "/");
                config->sync();
            }
        }
    }
}


void AppWizardDialog::projectNameChanged()
{
    // Location was already edited by hand => don't change
}


void AppWizardDialog::projectLocationChanged()
{
  // Jakob Simon-Gaarde: Got tired of the anoying bug with the appname/location confussion.
  // This version insures WYSIWYG and checks pathvalidity
  finalLoc_label->setText(dest_edit->url() + (dest_edit->url().right(1)=="/" ? "":"/") + appname_edit->text());
  TQDir qd(dest_edit->url());
  TQFileInfo fi(dest_edit->url() + "/" + appname_edit->text());
  if (!qd.exists() || appname_edit->displayText().isEmpty()||fi.exists())
  {
    if (!fi.exists() || appname_edit->displayText().isEmpty()) {
      finalLoc_label->setText(finalLoc_label->text() + i18n("invalid location", " (invalid)"));
    } else {
      finalLoc_label->setText(finalLoc_label->text() + i18n(" (dir/file already exists)"));
    }
    m_pathIsValid=false;
  } else {
    m_pathIsValid=true;
  }
  updateNextButtons();
}


void AppWizardDialog::insertCategoryIntoTreeView(const TQString &completeCategoryPath)
{
    kdDebug(9010) << "TemplateCategory: " << completeCategoryPath << endl;
    TQStringList categories = TQStringList::split("/", completeCategoryPath);
    TQString category ="";
    TQListViewItem* pParentItem=0;

    TQStringList::ConstIterator it;
    for (it = categories.begin(); it != categories.end(); ++it) {
        category = category + "/" + *it;
        TQListViewItem *item = m_categoryMap.find(category);
        if (!item) { // not found, create it
            if (!pParentItem)
                pParentItem = new KListViewItem(templates_listview,*it);
            else
                pParentItem = new KListViewItem(pParentItem,*it);

            pParentItem->setPixmap(0, SmallIcon("folder"));
            //pParentItem->setOpen(true);
            kdDebug(9010) << "Category: " << category << endl;
            m_categoryMap.insert(category,pParentItem);
            m_categoryItems.append(pParentItem);
        } else {
            pParentItem = item;
        }
    }
}


ApplicationInfo *AppWizardDialog::templateForItem(TQListViewItem *item)
{
    TQPtrListIterator<ApplicationInfo> it(m_appsInfo);
    for (; it.current(); ++it)
        if (it.current()->item == item)
            return it.current();

    return 0;
}

void AppWizardDialog::openAfterGeneration()
{
	TQString projectFile( finalLoc_label->text() + "/" + appname_edit->text().lower() + ".tdevelop" );

	// Read the DOM of the newly created project
	TQFile file( projectFile );
	if( !file.open( IO_ReadOnly ) )
		return;
	TQDomDocument projectDOM;

	int errorLine, errorCol;
	TQString errorMsg;
	bool success = projectDOM.setContent( &file, &errorMsg, &errorLine, &errorCol);
	file.close();
	if ( !success )
	{
		KMessageBox::sorry( 0, i18n("This is not a valid project file.\n"
				"XML error in line %1, column %2:\n%3")
				.arg(errorLine).arg(errorCol).arg(errorMsg));
		return;
	}

	// DOM Modifications go here
	DomUtil::writeMapEntry( projectDOM, "substmap", m_pCurrentAppInfo->subMap );

	//save the selected vcs
	KTrader::OfferList offers = KTrader::self()->query("TDevelop/VCSIntegrator", TQString("[X-TDevelop-VCS]=='%1'").arg(m_vcsForm->combo->currentText()));
	if (offers.count() == 1)
	{
		KService::Ptr service = offers.first();
		DomUtil::writeEntry(projectDOM, "/general/versioncontrol", service->property("X-TDevelop-VCSPlugin").toString());
	}

	// figure out what plugins we should disable by default
	TQString profileName = DomUtil::readEntry( projectDOM, "general/profile" );
	if ( profileName.isEmpty() )
	{
		TQString language = DomUtil::readEntry( projectDOM, "general/primarylanguage" );
		TQStringList keywords = DomUtil::readListEntry( projectDOM, "general/keywords", "keyword" );

		profileName = Settings::profileByAttributes( language, keywords );
	}

	ProfileEngine & engine = m_part->pluginController()->engine();
	Profile * profile = engine.findProfile( profileName );

	TQStringList disableList;
	Profile::EntryList disableEntryList = profile->list( Profile::ExplicitDisable );
	for ( Profile::EntryList::const_iterator it = disableEntryList.constBegin(); it != disableEntryList.constEnd(); ++it )
	{
		disableList << (*it).name;
	}

	TQStringList projectIgnoreparts = DomUtil::readListEntry( projectDOM, "/general/ignoreparts", "part" );
	projectIgnoreparts += disableList;
	DomUtil::writeListEntry( projectDOM, "/general/ignoreparts", "part", projectIgnoreparts );

	DomUtil::writeEntry( projectDOM, "/general/projectname", appname_edit->text() );

	// write the dom back
	if( !file.open( IO_WriteOnly ) )
		return;
	TQTextStream ts( &file );
	ts.setEncoding(TQTextStream::UnicodeUTF8);
	ts << projectDOM.toString(2);
	file.close();

	// open the new project
	m_part->core()->openProject( projectFile );

	// calculate the list of files to open after generation and use
	// timer to queue opening (so that files will not be opened before the project
	// which is also queued by ProjectManager )
	KURL::List urlsToOpen;
	TQStringList::Iterator it = m_pCurrentAppInfo->openFilesAfterGeneration.begin();
	for( ; it != m_pCurrentAppInfo->openFilesAfterGeneration.end(); ++it )
	{
		TQString fileName( *it );
		if ( !fileName.isNull() )
		{
			fileName = KMacroExpander::expandMacros(fileName, m_pCurrentAppInfo->subMap);
			urlsToOpen.append(KURL::fromPathOrURL(fileName));
		}
	}
	m_part->openFilesAfterGeneration(urlsToOpen);
}

void AppWizardDialog::pageChanged()
{
	kdDebug(9010) << "AppWizardDialog::pageChanged()" << endl;
	projectLocationChanged();
	if (currentPage() == m_lastPage)
		finishButton()->setDefault(true);


	//it is possible that project name was changed - we need to update all vcs integrator dialogs
	for (TQMap<int, VCSDialog*>::iterator it = m_integratorDialogs.begin();
		it != m_integratorDialogs.end(); ++it)
		(*it)->init(getProjectName(), getProjectLocation());
}

void AppWizardDialog::addTemplateToFavourites()
{
	addFavourite(templates_listview->currentItem());
}

void AppWizardDialog::addFavourite(TQListViewItem* item, TQString favouriteName)
{
	if(item->childCount())
		return;

	ApplicationInfo* info = templateForItem(item);

	if(!info->favourite)
	{
		info->favourite = new KIconViewItem(favourites_iconview,
											((favouriteName=="")?info->name:favouriteName),
											DesktopIcon("tdevelop"));

		info->favourite->setRenameEnabled(true);
	}
}

ApplicationInfo* AppWizardDialog::findFavouriteInfo(TQIconViewItem* item)
{
    TQPtrListIterator<ApplicationInfo> info(m_appsInfo);
    for (; info.current(); ++info)
        if (info.current()->favourite == item)
            return info.current();

	return 0;
}

void AppWizardDialog::favouritesIconViewClicked( TQIconViewItem* item)
{
	ApplicationInfo* info = findFavouriteInfo(item);
	templatesTreeViewClicked(info->item);
}

void AppWizardDialog::removeFavourite()
{
	TQIconViewItem* curFavourite = favourites_iconview->currentItem();

	//remove reference to favourite from associated appinfo
	TQPtrListIterator<ApplicationInfo> info(m_appsInfo);
	for (; info.current(); ++info)
	{
        if(info.current()->favourite && info.current()->favourite == curFavourite)
		{
			info.current()->favourite = 0;
		}
	}

	//remove favourite from iconview
	delete curFavourite;
	curFavourite=0;
	favourites_iconview->sort();	//re-arrange all items.
}

void AppWizardDialog::populateFavourites()
{
	TDEConfig* config = kapp->config();
	config->setGroup("AppWizard");

	//favourites are stored in config as a list of templates and a seperate
	//list of icon names.
	TQStringList templatesList = config->readPathListEntry("FavTemplates");
	TQStringList iconNamesList = config->readListEntry("FavNames");

	TQStringList::Iterator curTemplate = templatesList.begin();
	TQStringList::Iterator curIconName = iconNamesList.begin();
	while(curTemplate != templatesList.end())
	{
		TQPtrListIterator<ApplicationInfo> info(m_appsInfo);
		for (; info.current(); ++info)
		{
			if(info.current()->templateName == *curTemplate)
			{
				addFavourite(info.current()->item, *curIconName);
				break;
			}
		}
		curTemplate++;
		curIconName++;
	}
}

void AppWizardDialog::done(int r)
{
	//need to save the template for each favourite and
	//it's icon name.  We have a one list for the templates
	//and one for the names.

	TQStringList templatesList;
	TQStringList iconNamesList;

	//Built the stringlists for each template that has a favourite.
	TQPtrListIterator<ApplicationInfo> it(m_appsInfo);
	for (; it.current(); ++it)
	{
        if(it.current()->favourite)
		{
			templatesList.append(it.current()->templateName);
			iconNamesList.append(it.current()->favourite->text());
		}
	}

	TDEConfig* config = kapp->config();
	config->setGroup("AppWizard");
	config->writePathEntry("FavTemplates", templatesList);
	config->writeEntry("FavNames", iconNamesList);
	//config->writeEntry("CurrentTab", templates_tabwidget->currentPageIndex());
	config->sync();

	TQDialog::done(r);
}

void AppWizardDialog::templatesContextMenu(TQListViewItem* item, const TQPoint& point, int)
{
	if(item && !item->childCount())
		m_templatesMenu->popup(point);
}

void AppWizardDialog::favouritesContextMenu(TQIconViewItem* item, const TQPoint& point)
{
	if(item)
		m_favouritesMenu->popup(point);
}

void AppWizardDialog::setPermissions(const KArchiveFile *source, TQString dest)
{
	kdDebug(9010) << "AppWizardDialog::setPermissions(const KArchiveFile *source, TQString dest)" << endl;
	kdDebug(9010) << "	dest: " << dest << endl;

	if (source->permissions() & 00100)
	{
		kdDebug(9010) << "source is executable" << endl;
		TDEIO::UDSEntry entry;
		KURL kurl = KURL::fromPathOrURL(dest);
		if (TDEIO::NetAccess::stat(kurl, entry, 0))
		{
			KFileItem it(entry, kurl);
			int mode = it.permissions();
			kdDebug(9010) << "stat shows permissions: " << mode << endl;
			TDEIO::chmod(KURL::fromPathOrURL(dest), mode | 00100 );
		}
	}
}

void AppWizardDialog::setPermissions(const installFile &file)
{
	kdDebug(9010) << "AppWizardDialog::setPermissions(const installFile &file)" << endl;
	kdDebug(9010) << "	dest: " << file.dest << endl;

	TDEIO::UDSEntry sourceentry;
	KURL sourceurl = KURL::fromPathOrURL(file.source);
	if (TDEIO::NetAccess::stat(sourceurl, sourceentry, 0))
	{
		KFileItem sourceit(sourceentry, sourceurl);
		int sourcemode = sourceit.permissions();
		if (sourcemode & 00100)
		{
			kdDebug(9010) << "source is executable" << endl;
			TDEIO::UDSEntry entry;
			KURL kurl = KURL::fromPathOrURL(file.dest);
			if (TDEIO::NetAccess::stat(kurl, entry, 0))
			{
				KFileItem it(entry, kurl);
				int mode = it.permissions();
				kdDebug(9010) << "stat shows permissions: " << mode << endl;
				TDEIO::chmod(KURL::fromPathOrURL(file.dest), mode | 00100 );
			}
		}
	}
}

TQDict<KDevLicense> AppWizardDialog::licenses()
{
	return m_licenses;
}

void AppWizardDialog::loadLicenses()
{
	// kdDebug(9010) << "======================== Entering loadLicenses" << endl;
	KStandardDirs* dirs = TDEGlobal::dirs();
	dirs->addResourceType( "licenses", KStandardDirs::kde_default( "data" ) + "tdevelop/licenses/" );
	TQStringList licNames = dirs->findAllResources( "licenses", TQString(), false, true );

	TQStringList::Iterator it;
	for (it = licNames.begin(); it != licNames.end(); ++it)
	{
		TQString licPath( dirs->findResource( "licenses", *it ) );
		kdDebug(9010) << "Loading license file: " << licPath << endl;
		TQString licName = licPath.mid( licPath.findRev('/') + 1 );
		KDevLicense* lic = new KDevLicense( licName, licPath );
		m_licenses.insert( licName, lic );
	}
	// kdDebug(9010) << "======================== Done loadLicenses" << endl;
}

void AppWizardDialog::showTemplates(bool all)
{
	if (all)
	{
		TQListViewItemIterator it(templates_listview);
		while ( it.current() ) {
			it.current()->setVisible(true);
			++it;
		}
	}
	else
	{
		TQPtrListIterator<ApplicationInfo> ait(m_appsInfo);
		for (; ait.current(); ++ait)
		{
			ait.current()->item->setVisible(m_profileSupport->isInTemplateList(ait.current()->templateName));
		}

		TQDictIterator<TQListViewItem> dit(m_categoryMap);
		for (; dit.current(); ++dit)
		{
			//checking whether all children are not visible
			kdDebug(9010) << "check: " << dit.current()->text(0) << endl;
			bool visible = false;
			TQListViewItemIterator it(dit.current());
			while ( it.current() ) {
				if ((it.current()->childCount() == 0) && it.current()->isVisible())
				{
					kdDebug(9010) << "	visible: " << it.current()->text(0) << endl;
					visible = true;
					break;
				}
				++it;
			}
			dit.current()->setVisible(visible);
		}
		checkAndHideItems(templates_listview);
	}
}

void AppWizardDialog::checkAndHideItems(TQListView *view)
{
	TQListViewItem *item = view->firstChild();
	while (item)
	{
		if (!m_categoryItems.contains(item))
			continue;
		checkAndHideItems(item);
		item = item->nextSibling();
	}
}

bool AppWizardDialog::checkAndHideItems(TQListViewItem *item)
{
	if (!m_categoryItems.contains(item))
		return !item->isVisible();
	TQListViewItem *child = item->firstChild();
	bool hide = true;
	while (child)
	{
		hide = hide && checkAndHideItems(child);
		child = child->nextSibling();
	}
	kdDebug(9010) << "check_: " << item->text(0) << " hide: " <<  hide << endl;
	if (hide)
	{
		item->setVisible(false);
		return true;
	}
	return false;
}

#include "appwizarddlg.moc"

// kate: indent-width 4; replace-tabs off; tab-width 4; space-indent off;