[419cd4]: src / hugin1 / ptbatcher / Batch.cpp Maximize Restore History

Download this file

Batch.cpp    1014 lines (949 with data), 29.6 kB

   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
// -*- c-basic-offset: 4 -*-
/** @file Batch.cpp
*
* @brief Batch processor for Hugin
*
* @author Marko Kuder <marko.kuder@gmail.com>
*
* $Id: Batch.cpp 3322 2008-08-18 1:10:07Z mkuder $
*
* 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 software 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 software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "Batch.h"
#include <wx/stdpaths.h>
BEGIN_EVENT_TABLE(Batch, wxFrame)
EVT_END_PROCESS(-1, Batch::OnProcessTerminate)
END_EVENT_TABLE()
DEFINE_EVENT_TYPE(EVT_BATCH_FAILED)
Batch::Batch(wxFrame* parent, wxString path, bool bgui) : wxFrame(parent,wxID_ANY,_T("Batch")){
//default flag settings
parallel = false;
deleteFiles = false;
shutdown = false;
overwrite = true;
verbose = false;
gui = bgui;
m_cancelled = false;
m_paused = false;
m_running = false;
m_clearedInProgress = false;
m_lastFile = wxT("");
// Required to access the preferences of hugin
//SetAppName(wxT("hugin"));
// setup the environment for the different operating systems
wxConfigBase* config=wxConfigBase::Get();
#if defined __WXMSW__
wxString huginExeDir = getExePath(path);
wxString huginRoot;
wxFileName::SplitPath(huginExeDir, &huginRoot, NULL, NULL);
progs = getPTProgramsConfig(huginExeDir, config);
progsAss = getAssistantProgramsConfig(huginExeDir, config);
#else
// add the locale directory specified during configure
progs = getPTProgramsConfig(wxT(""), config);
progsAss = getAssistantProgramsConfig(wxT(""), config);
#endif
}
void Batch::AddAppToBatch(wxString app)
{
Project *newApp = new Project(app);
m_projList.Add(newApp);
}
void Batch::AddProjectToBatch(wxString projectFile, wxString outputFile,Project::Target target)
{
wxFileName projectName(projectFile);
wxFileName outName(outputFile);
projectName.Normalize();
outName.Normalize();
if(outputFile.Cmp(_T(""))!=0 || target==Project::DETECTING)
{
Project *proj = new Project(projectName.GetFullPath(),outName.GetFullPath(),target);
m_projList.Add(proj);
/*if(gui)
((wxFrame*)GetParent())->SetStatusText(_T("Added project ")+projectFile+_T(" with output ")+outputFile);
else if(verbose)
cout << "Added project " << projectFile.char_str() << " with output " << outputFile.char_str() << endl;*/
}
else
{ //on output set as "", it defaults to same path and name as project file
Project *proj = new Project(projectName.GetFullPath(),projectName.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR) + projectName.GetName());
m_projList.Add(proj);
/*if(gui)
((wxFrame*)GetParent())->SetStatusText(_T("Added project ")+projectFile);
else if(verbose)
cout << "Added project " << projectFile.char_str() << endl;*/
}
}
bool Batch::AllDone()
{
for(unsigned int i=0; i<m_projList.GetCount(); i++)
{
if(m_projList.Item(i).status==Project::WAITING ||
m_projList.Item(i).status==Project::RUNNING ||
m_projList.Item(i).status==Project::PAUSED)
{
return false;
}
}
return true;
}
void Batch::AppendBatchFile(wxString file)
{
wxFileName aFile(file);
if(aFile.FileExists())
{
m_lastFile = file;
aFile.GetTimes(NULL,NULL,&m_lastmod);
wxFileInputStream fileStream(file);
wxString projectName = _T("");
wxTextInputStream textStream(fileStream);
//TO-DO: batch file error checking?
//first line in file is idGenerator, we save it a temp variable, cause it gets set when adding projects
long idGenTemp = 1;
textStream.ReadLine().ToLong(&idGenTemp);
//then for each project: project path, prefix, id, status, skip
while((projectName = textStream.ReadLine()).Cmp(wxT(""))!=0)
{
//we add project to internal list
wxString line=textStream.ReadLine();
if(line.IsEmpty())
{
AddProjectToBatch(projectName,wxT(""),Project::DETECTING);
}
else
{
AddProjectToBatch(projectName,line);
};
textStream.ReadLine().ToLong(&m_projList.Last().id);
long status;
textStream.ReadLine().ToLong(&status);
//if status was RUNNING or PAUSED, we set it to FAILED
if(status==(long)Project::RUNNING || status==(long)Project::PAUSED)
status=(long)Project::FAILED;
m_projList.Last().status = (Project::Status)status;
if(textStream.ReadLine().StartsWith(_T("T")))
m_projList.Last().skip = true;
}
//we set the id generator we got from file
Project::idGenerator = idGenTemp;
}
}
void Batch::CancelBatch()
{
m_cancelled = true;
for(int i=0; i<GetRunningCount(); i++)
CancelProject(i);
m_running = false;
}
void Batch::CancelProject(int index)
{
wxCommandEvent event;
if(GetRunningCount()==1)
m_paused = false;
m_stitchFrames.Item(index)->OnCancel(event);
if(GetRunningCount()==0)
m_running = false;
}
void Batch::ChangePrefix(int index, wxString newPrefix)
{
m_projList.Item(index).prefix = newPrefix;
}
bool Batch::CheckProjectExistence() //used only in console version
{
#ifdef __WXMSW__ //on windows we run a loop every second to check if running processes are still active
bool exist = true;
HANDLE process;
DWORD exitState;
while(exist)
{
exist = false;
for(unsigned int i=0; i<m_stitchFrames.GetCount(); i++)
{
try
{
process = OpenProcess(PROCESS_QUERY_INFORMATION,true,m_stitchFrames.Item(i)->GetProcessId());
GetExitCodeProcess(process,&exitState);
}
catch(::exception e)
{
exitState=1;
}
if(exitState==STILL_ACTIVE)
exist=true;
else if(exitState!=0)
SetStatus(GetIndex(m_stitchFrames.Item(i)->GetProjectId()),Project::FAILED);
CloseHandle(process);
}
wxSleep(1);
}
#else //not __WXMSW__, on Linux we wait for each of the processes to complete
int status;
int pid;
for(unsigned int i=0; i<m_stitchFrames.GetCount(); i++)
{
pid = m_stitchFrames.Item(i)->GetProcessId();
if(waitpid(pid,&status,0)==-1)
SetStatus(GetIndex(m_stitchFrames.Item(i)->GetProjectId()),Project::FAILED); //we set to failed if waitpid terminated with an error
if(!WIFEXITED(status) || WEXITSTATUS(status)!=0)
SetStatus(GetIndex(m_stitchFrames.Item(i)->GetProjectId()),Project::FAILED); //we set to failed if child terminated abnormally or with a bad exit code
}
#endif
wxProcessEvent event;
for(int i=m_stitchFrames.GetCount()-1; i>=0; i--)
{
event.SetId(m_stitchFrames.Item(i)->GetProjectId());
if(GetStatus(GetIndex(m_stitchFrames.Item(i)->GetProjectId()))==Project::FAILED)
event.SetTimestamp(-1); //a failed exit code cannot be set to a wxWidgets event, so we fake it inside event's timestamp
OnProcessTerminate(event);
}
return true;
}
int Batch::ClearBatch()
{
if(m_stitchFrames.GetCount()!=0)
{
if(gui)
{
wxMessageDialog message(this, _("Cannot clear batch in progress.\nDo you want to cancel it?"), _("In progress"), wxYES | wxCANCEL | wxICON_INFORMATION);
if(message.ShowModal()==wxID_YES)
{
CancelBatch();
//we set a flag so we don't process terminating events
m_clearedInProgress = true;
Project::idGenerator=1;
m_projList.Clear();
if(gui)
((wxFrame*)GetParent())->SetStatusText(_("Cleared batch."));
else if(verbose)
cout << "Cleared batch." << endl;
return 2;
}
}
else if(verbose)
cout << "Error: Cannot clear batch in progress." << endl;
return 1;
//TO-DO: return
/*wxMessageDialog message(this, _T("Cannot clear batch in progress.\nDo you want to cancel it?"), _T("In progress"), wxYES | wxCANCEL | wxICON_INFORMATION);
if(message.ShowModal()==wxID_YES)
{
OnButtonCancel(event);
}*/
}
else
{
Project::idGenerator=1;
m_projList.Clear();
if(gui)
((wxFrame*)GetParent())->SetStatusText(_("Cleared batch."));
else if(verbose)
cout << "Cleared batch." << endl;
return 0;
}
}
bool Batch::CompareProjectsInLists(int stitchListIndex, int batchListIndex)
{
return m_stitchFrames.Item(stitchListIndex)->GetProjectId() == m_projList.Item(batchListIndex).id;
}
int Batch::GetFirstAvailable()
{
unsigned int i = 0;
while(i<m_projList.Count())
{
if(m_projList.Item(i).skip || m_projList.Item(i).status!=Project::WAITING)
i++;
else
break;
}
if((m_projList.Count() == 0) || (i == m_projList.Count()))
{
//no projects are available anymore
return -1;
}
else
{
return i;
}
}
int Batch::GetIndex(int id)
{
for(unsigned int i=0; i<m_projList.GetCount(); i++)
{
if(m_projList.Item(i).id==id)
return i;
}
return -1;
}
Project* Batch::GetProject(int index)
{
return (Project*)&m_projList.Item(index);
}
int Batch::GetProjectCount()
{
return m_projList.GetCount();
}
int Batch::GetProjectCountByPath(wxString path)
{
int count = 0;
for(unsigned int i=0; i<m_projList.GetCount(); i++)
{
if(!m_projList.Item(i).skip && (path.Cmp(m_projList.Item(i).path)==0))
count++;
}
return count;
}
int Batch::GetRunningCount()
{
return m_stitchFrames.GetCount();
}
Project::Status Batch::GetStatus(int index)
{
if((unsigned int)index<m_projList.GetCount())
return m_projList.Item(index).status;
else if(gui)
wxMessageBox(wxString::Format(_("Error: Could not get status, project with index %d is not in list."),index),_("Error!"),wxOK | wxICON_INFORMATION );
else if(verbose)
cout << "Error: Could not get status, project with index " << index << " is not in list." << endl;
return Project::MISSING;
}
bool Batch::IsPaused()
{
return m_paused;
}
void Batch::ListBatch()
{
if(m_projList.GetCount() == 0)
cout << "Batch is empty." << endl;
else
{
cout << "List of projects in batch:" << endl <<
"[ID] [project path] [output filename] [status]" << endl <<
"-------------------------------------" << endl;
for(unsigned int i=0; i<m_projList.GetCount(); i++)
{
cout << m_projList.Item(i).id << " " << (const char*)m_projList.Item(i).path.char_str() << " " << (const char*)m_projList.Item(i).prefix.char_str()
<< " " << (const char*)m_projList.Item(i).GetStatusText().char_str() << endl;
}
}
}
int Batch::LoadBatchFile(wxString file)
{
int clearCode = ClearBatch();
if(clearCode==0)
{
AppendBatchFile(file);
return 0;
}
else if(clearCode==2)
{
AppendBatchFile(file);
return 2;
}
else if(gui)
wxMessageBox(_("Error: Could not load batch file."));
else if(verbose)
cout << "Error: Could not load batch file." << endl;
return 1;
}
int Batch::LoadTemp()
{
wxDir* workingDir = new wxDir(wxStandardPaths::Get().GetUserConfigDir());
wxString pending;
wxString fileTemp = _T(".ptbt*");
wxString temp = _T("");
//we check for existing temporary files
if(workingDir->GetFirst(&temp,fileTemp,wxDIR_FILES | wxDIR_HIDDEN))
{
//we find the last existing tempfile (there should be at most two, but we check for multiple just in case)
while(workingDir->GetNext(&pending))
{
wxFileName tempFile(temp);
wxFileName pendingFile(pending);
wxDateTime* create1 = new wxDateTime();
wxDateTime* create2 = new wxDateTime();
if(tempFile.FileExists() && pendingFile.FileExists())
{
tempFile.GetTimes(NULL,NULL,create1);
pendingFile.GetTimes(NULL,NULL,create2);
if(create2->IsLaterThan(*create1))
{
wxRemoveFile(temp);
temp=wxString(pending);
}
}
else
{
//wxMessageBox( _T("Error reading temporary file"),_T("Error!"),wxOK | wxICON_INFORMATION );
return 1;
}
}
}
//we load the data from the temp file
AppendBatchFile(workingDir->GetName()+wxFileName::GetPathSeparator()+temp);
if(verbose && !gui)
cout << "Loaded temp file." << endl;
return 0;
}
bool Batch::NoErrors()
{
for(unsigned int i=0; i<m_projList.GetCount(); i++)
{
if(m_projList.Item(i).status==Project::FAILED)
{
return false;
}
}
return true;
}
void Batch::OnProcessTerminate(wxProcessEvent & event)
{
//we find the right pointer to remove
unsigned int i = 0;
while(i < m_stitchFrames.GetCount() &&
m_stitchFrames.Item(i)->GetProjectId()!=event.GetId())
{
i++;
}
m_stitchFrames.RemoveAt(i);
if(m_clearedInProgress)
{
if(m_stitchFrames.GetCount()==0)
{
m_paused = false;
m_running = false;
m_cancelled = false;
m_clearedInProgress = false;
}
}
else
{
if(m_stitchFrames.GetCount()==0)
m_paused = false;
i = GetIndex(event.GetId());
if (event.GetExitCode() != 0 || event.GetTimestamp()==-1) //timestamp is used as a fake exit code because it cannot be set manually
{
m_projList.Item(i).status=Project::FAILED;
struct FailedProject failedProject;
failedProject.project=m_projList.Item(i).path;
failedProject.logfile=wxEmptyString;
//get filename for automatic saving of log file
wxFileName logFile(m_projList.Item(i).path);
logFile.MakeAbsolute();
logFile.SetExt(wxT("log"));
wxString name=logFile.GetName();
unsigned int i=1;
while(logFile.FileExists() && i<1000)
{
logFile.SetName(wxString::Format(wxT("%s_%d"),name.c_str(),i));
i++;
};
if(i<1000)
{
//now save log file
if(((RunStitchFrame*)(event.GetEventObject()))->SaveLog(logFile.GetFullPath()))
failedProject.logfile=logFile.GetFullPath();
};
//remember failed project
m_failedProjects.push_back(failedProject);
}
else
m_projList.Item(i).status=Project::FINISHED;
if(!m_cancelled && !m_paused)
{
if(AllDone())
{
m_running = false;
if(NoErrors())
{
if(gui)
//SetStatusText(_T("Project \"")+m_projList.Item(i).path+_T("\" finished. Batch successfully completed."));
SetStatusText(_("Batch successfully completed."));
else
//cout << "Project \"" << m_projList.Item(i).path.char_str() << "\" finished. Batch successfully completed." << endl;
cout << "Batch successfully completed." << endl;
}
else
{
if(gui)
{
SetStatusText(_("Batch completed with errors."));
if(!shutdown)
{
//notify parent, that at least one project failed
wxCommandEvent e(EVT_BATCH_FAILED,wxID_ANY);
GetParent()->AddPendingEvent(e);
};
}
else
//cout << "Project \"" << m_projList.Item(i).path.char_str() << "\" finished. Batch completed with errors." << endl;
cout << "Batch completed with errors." << endl;
}
if(shutdown) //after we are finished we turn off the computer if checked
{
if(gui)
{
wxProgressDialog progress(_("Initializing shutdown..."), _("Shutting down..."),49,this,
wxPD_AUTO_HIDE | wxPD_SMOOTH | wxPD_APP_MODAL | wxPD_CAN_ABORT | wxPD_CAN_SKIP);
progress.Fit();
int i = 0;
bool skip = false;
while(progress.Update(i, _("Shutting down..."),&skip))
{
if(skip || i==50)
{
/*wxMessageDialog message(this,_T("Krneksa"));
message.ShowModal();
break;*/
wxShutdown(wxSHUTDOWN_POWEROFF);
}
i++;
#if defined __WXMSW__
Sleep(200);
#else
sleep(200);
#endif
}
progress.Close();
}
else
{
if(!wxShutdown(wxSHUTDOWN_POWEROFF))
{
if(gui)
#ifdef __WXMSW__
wxMessageBox(_("Error shutting down."));
#else
wxMessageBox(_("Error shutting down. Do you have root privileges?"));
#endif
else
#ifdef __WXMSW__
cout << "Error shutting down." << endl;
#else
cout << "Error shutting down. Do you have root privileges?" << endl;
#endif
}
}
}
}
else
{
if(parallel) //if we are running in parallel
{
//the last executed process in parallel runs next
if(GetRunningCount() == 0)
{
//SetStatusText(_T("Project \""+m_projList.Item(i).path)+_T("\" finished. Running next project..."));
RunNextInBatch();
}
else
{
//SetStatusText(_T("Project \""+m_projList.Item(i).path)+_T("\" finished. Waiting for all in parallel to complete..."));
}
}
else
{
//SetStatusText(_T("Project \""+m_projList.Item(i).path)+_T("\" finished. Running next project..."));
RunNextInBatch();
}
}
}
else
{ //after all processes have ended on a cancel, we reset the boolean back to false
//if(stitchFrames.GetCount()==0)
if(GetRunningCount()==0)
{
m_cancelled=false;
}
}
}
}
bool Batch::OnStitch(wxString scriptFile, wxString outname, int id) //was previously without parameter
{
//if( parser.GetParamCount() == 0 && wxIsEmpty(scriptFile))
//{
if(wxIsEmpty(scriptFile))
{
wxString defaultdir = wxConfigBase::Get()->Read(wxT("/actualPath"),wxT(""));
wxFileDialog dlg(0,
_("Specify project source project file"),
defaultdir, wxT(""),
_("Project files (*.pto,*.ptp,*.pts,*.oto)|*.pto;*.ptp;*.pts;*.oto;|All files (*)|*"),
wxOPEN, wxDefaultPosition);
dlg.SetDirectory(wxConfigBase::Get()->Read(wxT("/actualPath"),wxT("")));
if (dlg.ShowModal() == wxID_OK) {
wxConfig::Get()->Write(wxT("/actualPath"), dlg.GetDirectory()); // remember for later
//scriptFile = dlg.GetPath();
wxFileDialog dlg2(0,_("Specify output prefix"),
wxConfigBase::Get()->Read(wxT("/actualPath"),wxT("")),
wxT(""), wxT(""),
wxSAVE, wxDefaultPosition);
dlg2.SetDirectory(wxConfigBase::Get()->Read(wxT("/actualPath"),wxT("")));
if (dlg2.ShowModal() == wxID_OK) {
//wxConfig::Get()->Write(wxT("/actualPath"), dlg2.GetDirectory()); // remember for later
outname = dlg2.GetPath();
} else { // bail
wxLogError( _("No output prefix specified"));
return false;
}
scriptFile = dlg.GetPath();
} else { // bail
return false;
wxLogError( _("No project files specified"));
}
}
if(!gui)
cout << "Stitching with input file " << (const char *)scriptFile.mb_str(wxConvLocal) << "..." << endl;
// check output filename
wxFileName outfn(outname);
wxString ext = outfn.GetExt();
// remove extension if it indicates an image file
if (ext.CmpNoCase(wxT("jpg")) == 0 || ext.CmpNoCase(wxT("jpeg")) == 0 ||
ext.CmpNoCase(wxT("tif")) == 0 || ext.CmpNoCase(wxT("tiff")) == 0 ||
ext.CmpNoCase(wxT("png")) == 0 || ext.CmpNoCase(wxT("exr")) == 0 ||
ext.CmpNoCase(wxT("pnm")) == 0 || ext.CmpNoCase(wxT("hdr")) == 0)
{
outfn.ClearExt();
outname = outfn.GetFullPath();
}
RunStitchFrame *stitchFrame = new RunStitchFrame(this, wxT("Hugin Stitcher"), wxDefaultPosition, wxSize(640,600)); //,wxGetApp().m_projList, projListBox );
//stitchFrame->SetEventHandler(this);
stitchFrame->SetProjectId(id);
if(verbose && gui)
{
stitchFrame->Show( true );
wxTheApp->SetTopWindow( stitchFrame );
}
wxFileName basename(scriptFile);
stitchFrame->SetTitle(wxString::Format(_("%s - Stitching"), basename.GetName().c_str()));
if(overwrite)
stitchFrame->m_stitchPanel->SetOverwrite(true);
bool n = stitchFrame->StitchProject(scriptFile, outname, progs);
if(n)
m_stitchFrames.Add(stitchFrame);
else
stitchFrame->Close();
/*if (parser.Found(wxT("d")) ) {
wxRemoveFile(scriptFile);
}*/
return n;
}
bool Batch::OnDetect(wxString scriptFile, int id)
{
if(!gui)
cout << "Running assistant with input file " << (const char *)scriptFile.mb_str(wxConvLocal) << "..." << endl;
RunStitchFrame *stitchFrame = new RunStitchFrame(this, wxT("Hugin Assistant"), wxDefaultPosition, wxSize(640,600));
stitchFrame->SetProjectId(id);
if(verbose && gui)
{
stitchFrame->Show( true );
wxTheApp->SetTopWindow( stitchFrame );
}
wxFileName basename(scriptFile);
stitchFrame->SetTitle(wxString::Format(_("%s - Assistant"), basename.GetName().c_str()));
bool n = stitchFrame->DetectProject(scriptFile, progsAss);
if(n)
m_stitchFrames.Add(stitchFrame);
else
stitchFrame->Close();
return n;
}
void Batch::PauseBatch()
{
if(!m_paused)
{
m_paused = true;
for(int i=0; i<GetRunningCount(); i++)
{
m_stitchFrames.Item(i)->m_stitchPanel->PauseStitch();
}
for(unsigned int i=0; i<m_projList.GetCount(); i++)
{
if(m_projList.Item(i).status==Project::RUNNING)
m_projList.Item(i).status=Project::PAUSED;
}
}
else
{
m_paused = false;
for(int i=0; i<GetRunningCount(); i++)
{
m_stitchFrames.Item(i)->m_stitchPanel->ContinueStitch();
}
for(unsigned int i=0; i<m_projList.GetCount(); i++)
{
if(m_projList.Item(i).status==Project::PAUSED)
m_projList.Item(i).status=Project::RUNNING;
}
}
}
void Batch::RemoveProject(int id)
{
int index;
if((index=GetIndex(id)) != -1)
{
RemoveProjectAtIndex(GetIndex(id));
}
else if(gui)
wxMessageBox(wxString::Format(_("Error removing, project with id %d is not in list."),id),_("Error!"),wxOK | wxICON_INFORMATION );
else if(verbose)
cout << "Error: Project with id " << id << " is not in list." << endl;
}
void Batch::RemoveProjectAtIndex(int selIndex)
{
//we delete only successful project files and no applications
if(deleteFiles
&& m_projList.Item(selIndex).id>=0
&& m_projList.Item(selIndex).status==Project::FINISHED)
{
wxFileName file(m_projList.Item(selIndex).path);
if(file.FileExists())
{
if(!wxRemoveFile(file.GetFullPath()))
{
if(gui)
wxMessageBox( _("Error: Could not delete project file ")+file.GetFullPath(),_("Error!"),wxOK | wxICON_INFORMATION );
else if(verbose)
cout << "Error: Could not delete project file " << (const char*)file.GetFullPath().char_str() << endl;
}
}
}
m_projList.RemoveAt(selIndex);
if(m_projList.GetCount()==0) //reset the id generator on empty list
Project::idGenerator=1;
}
void Batch::RunBatch()
{
if(!gui && m_projList.GetCount() == 0)
cout << "Batch is empty." << endl;
else if(!m_running)
{
m_failedProjects.clear();
if(gui)
((wxFrame*)GetParent())->SetStatusText(_("Running batch..."));
else
cout << "Running batch..." << endl;
bool cancelRun = false;
//check for missing project files
wxFileName file = wxFileName();
for(unsigned int i=0; i<m_projList.Count(); i++)
{
if(m_projList.Item(i).id >= 0) //we don't check file existence for command line instructions
{
file.Assign(m_projList.Item(i).path);
if(!file.FileExists())
{
/*wxMessageDialog message(this,_T("Project file \"")+m_projList.Item(i).path+_T("\" is missing, project will not be executed."), _T("Project missing"),
wxOK | wxCANCEL | wxICON_INFORMATION );
if(message.ShowModal() != wxID_OK)
{
cancelRun = true;
}*/
}
}
}
//bool value;
//if we didn't cancel and not all project are marked for skip
if(!cancelRun)
{
m_running = true;
RunNextInBatch();
}
}
else if(gui)
((wxFrame*)GetParent())->SetStatusText(_("Batch already in progress."));
}
void Batch::RunNextInBatch()
{
//((wxToolBar*)FindWindow(TOOLBAR))->ToggleTool(TOOLPAUSE,false);
bool value;
bool repeat = true;
unsigned int i;
while(((i=GetFirstAvailable())!=-1) && repeat)
{
//execute command line instructions
if(m_projList.Item(i).id<0)
{
//if(wxFile::Exists(_T("h:\\pictures\\some2.tif")))
// wxMessageBox( _T("y"),_T("y"),wxOK | wxICON_INFORMATION );
if(gui)
SetStatusText(_("Running command \"")+m_projList.Item(i).path+_T("\""));
else
cout << "Running command \"" << (const char*)m_projList.Item(i).path.char_str() << "\"" << endl;
m_projList.Item(i).status=Project::RUNNING;
if(!gui) //we create a fake stitchFrame, so program waits for app to complete
{
//cout << "creating stitchframe" << endl;
RunStitchFrame *stitchFrame = new RunStitchFrame(this, wxT("Hugin Stitcher"), wxDefaultPosition, wxSize(640,600));
stitchFrame->SetProjectId(m_projList.Item(i).id);
//cout << "adding" << endl;
repeat = false;
int pid = wxExecute(m_projList.Item(i).path, wxEXEC_ASYNC);
stitchFrame->SetProcessId(pid);
m_stitchFrames.Add(stitchFrame);
//cout << "pid is " << pid << endl;
}
else
{
if(wxExecute(m_projList.Item(i).path, wxEXEC_SYNC)==0)
m_projList.Item(i).status=Project::FINISHED;
else
m_projList.Item(i).status=Project::FAILED;
}
}
else
{
if(!parallel) //we run in sequence
{
//wxMessageBox( _T("2"),_T("2"),wxOK | wxICON_INFORMATION );
m_projList.Item(i).status=Project::RUNNING;
//SetStatusText(_T("Stitching project \"")+m_projList.Item(i).path+_T("\""));
m_running = true;
if(m_projList.Item(i).target==Project::STITCHING)
{
value = OnStitch(m_projList.Item(i).path, m_projList.Item(i).prefix, m_projList.Item(i).id);
}
else
{
value = OnDetect(m_projList.Item(i).path,m_projList.Item(i).id);
};
if(!value)
m_projList.Item(i).status=Project::FAILED;
else
repeat = false;
}
else
{
while((i = GetFirstAvailable())!=-1)
{
//projListBox->Select(i); //select the currently processing project
if(m_projList.Item(i).id<0)
break;
m_projList.Item(i).status=Project::RUNNING;
//SetStatusText(_T("Stitching..."));
m_running = true;
if(m_projList.Item(i).target==Project::STITCHING)
{
value = OnStitch(m_projList.Item(i).path, m_projList.Item(i).prefix, m_projList.Item(i).id);
}
else
{
value = OnDetect(m_projList.Item(i).path,m_projList.Item(i).id);
};
if(!value)
m_projList.Item(i).status=Project::FAILED;
else
repeat = false;
}
//projListMutex->Unlock();
}
}
}
if(AllDone())
m_running = false;
else if(!gui)
CheckProjectExistence();
}
void Batch::SaveBatchFile(wxString file)
{
wxFileOutputStream fileStream(file);
wxTextOutputStream textStream(fileStream);
//we write current idGenerator to file
wxString line = _T("");
line << Project::idGenerator;
textStream.WriteString(line+_T("\n"));
//then for each project: project path, prefix, id, status, skip
for(unsigned int i = 0; i< m_projList.GetCount(); i++)
{
textStream.WriteString(m_projList.Item(i).path+_T("\n"));
if(m_projList.Item(i).target==Project::STITCHING)
{
textStream.WriteString(m_projList.Item(i).prefix+_T("\n"));
}
else
{
textStream.WriteString(_T("\n"));
};
line = _T("");
line << m_projList.Item(i).id;
textStream.WriteString(line+_T("\n"));
line = _T("");
line << m_projList.Item(i).status;
textStream.WriteString(line+_T("\n"));
if(m_projList.Item(i).skip)
textStream.WriteString(_T("T\n"));
else
textStream.WriteString(_T("F\n"));
}
fileStream.Close();
m_lastFile = file;
wxFileName aFile(file);
aFile.GetTimes(NULL,NULL,&m_lastmod);
}
void Batch::SaveTemp()
{
//wxMessageBox( _T("saving temp file"),_T(""),wxOK | wxICON_INFORMATION );
wxDir* workingDir = new wxDir(wxStandardPaths::Get().GetUserConfigDir());
wxString fileTemp = _T(".ptbt*");
//we get the old temp file
fileTemp = workingDir->FindFirst(workingDir->GetName(),fileTemp,wxDIR_FILES | wxDIR_HIDDEN);
wxFileName oldFile(fileTemp);
//we alternate between 0 and 1
wxString suffix;
if(fileTemp.EndsWith(_T("0")))
suffix = _T("1");
else
suffix = _T("0");
SaveBatchFile(wxStandardPaths::Get().GetUserConfigDir()+wxFileName::GetPathSeparator()+_T(".ptbt")+suffix);
if(verbose && !gui)
cout << "Saved temp file." << endl;
//we remove the previous temp file
if(oldFile.FileExists())
wxRemoveFile(fileTemp);
}
void Batch::SetStatus(int index,Project::Status status)
{
if((unsigned int)index<m_projList.GetCount())
m_projList.Item(index).status = status;
else if(gui)
wxMessageBox(wxString::Format(_("Error: Could not set status, project with index %d is not in list."),index),_("Error!"),wxOK | wxICON_INFORMATION );
else if(verbose)
cout << "Error: Could not set status, project with index " << index << " is not in list." << endl;
}
void Batch::SwapProject(int index)
{
Project* proj = m_projList.Detach(index+1);
m_projList.Insert(proj,index);
}
void Batch::ShowOutput(bool isVisible)
{
for(unsigned int i=0;i<m_stitchFrames.Count();i++)
{
m_stitchFrames.Item(i)->Show(isVisible);
};
};
wxString Batch::GetFailedProjectName(unsigned int i)
{
if(i>=0 && i<m_failedProjects.size())
return m_failedProjects[i].project;
else
return wxEmptyString;
};
wxString Batch::GetFailedProjectLog(unsigned int i)
{
if(i>=0 && i<m_failedProjects.size())
return m_failedProjects[i].logfile;
else
return wxEmptyString;
};