-
Notifications
You must be signed in to change notification settings - Fork 5
/
frmSeparateMergeChap.cs
executable file
·501 lines (420 loc) · 17.4 KB
/
frmSeparateMergeChap.cs
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
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using MarkAble2.Properties;
namespace MarkAble2
{
public partial class frmSeparateMergeChap : Form
{
public frmSeparateMergeChap()
{
InitializeComponent();
}
//this is set to a ridiculous length unless adjusted
private TimeSpan MaxDurationForSampleRate = new TimeSpan(50, 0, 0, 0); //50 days
private bool DoneIt;
private void frmSeparateMergeChap_Activated(object sender, EventArgs e)
{
if (!DoneIt)
{
DoneIt = true;
if (Global.Options.Encoder == Global.EncodeToTypes.AAC)
{
double maxsecs = (double)Int32.MaxValue / (double)Global.SampleRate;
long maxticks = Convert.ToInt64(TimeSpan.TicksPerSecond * maxsecs);
MaxDurationForSampleRate = new TimeSpan(maxticks);
}
if (Global.UnRippedFiles.Count > 0)
{
nudFiles.Maximum = Global.UnRippedFiles.Count;
int numFiles = 1;
while ((CheckFileSize(numFiles) != FileSizeChecks.OK) && (numFiles < Global.UnRippedFiles.Count))
{
Application.DoEvents();
numFiles++;
}
nudFiles.Value = numFiles;
ShowFileSize();
}
}
}
private void SetBookImage()
{
string anImage = Global.UnRippedFiles.BookImage;
if (!String.IsNullOrEmpty(anImage) && File.Exists(anImage))
{
try
{
Image imgsmall = Global.MakeSmallImage(anImage);
picBookImage.Image = imgsmall;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Resources.Unable_to_load_image_exclaim);
}
}
}
private void nudFiles_ValueChanged(object sender, EventArgs e)
{
ShowFileSize();
}
private void ShowFileSize()
{
if ((Global.UnRippedFiles != null) && (Global.UnRippedFiles.Count > 0))
{
double totsize = Global.UnRippedFiles.TotalSize() / 1E6; //this should give MB
TimeSpan totduration = Global.UnRippedFiles.TotalDuration();
if (totsize > 0)
{
if (nudFiles.Value == 1)
{
var filebreakdown = new StringBuilder(Resources.File_will_be_totsize_MB_in_size_duration_hours);
filebreakdown.Replace("<totsize>", totsize.ToString("#,##0.0"));
filebreakdown.Replace("<duration>", totduration.TotalHours.ToString("#,##0.0"));
labFileBreakdown.Text = filebreakdown.ToString();
}
else
{
totsize = totsize/(double) nudFiles.Value;
double newticks = (double) totduration.Ticks/(double) nudFiles.Value;
long longticks = Convert.ToInt64(newticks);
totduration = new TimeSpan(longticks);
if (totduration > TimeSpan.Zero)
{
var filebreakdown =
new StringBuilder(Resources.Each_file_will_be_approx_totsize_MB_in_size_duration_duration);
filebreakdown.Replace("<totsize>", totsize.ToString("#,##0"));
filebreakdown.Replace("<duration>", totduration.TotalHours.ToString("#,##0.0"));
labFileBreakdown.Text = filebreakdown.ToString();
}
else
{
var filebreakdown = new StringBuilder(Resources.Each_file_will_be_approx_totsize_MB_in_size);
filebreakdown.Replace("<totsize>", totsize.ToString("#,##0"));
labFileBreakdown.Text = filebreakdown.ToString();
}
}
}
else
{
if (nudFiles.Value == 1)
{
var filebreakdown = new StringBuilder(Resources.File_will_have_duration_duration_hours_);
filebreakdown.Replace("<duration>", totduration.TotalHours.ToString("#,##0.0"));
labFileBreakdown.Text = filebreakdown.ToString();
}
else
{
double newticks = (double) totduration.Ticks/(double) nudFiles.Value;
long longticks = Convert.ToInt64(newticks);
totduration = new TimeSpan(longticks);
var filebreakdown = new StringBuilder(Resources.Each_file_will_have_duration_duration_hours);
filebreakdown.Replace("<duration>", totduration.TotalHours.ToString("#,##0.0"));
labFileBreakdown.Text = totduration > TimeSpan.Zero
? filebreakdown.ToString()
: Resources.Unable_to_determine_file_size_and_duration;
}
}
}
}
private void butStart_Click(object sender, EventArgs e)
{
SetListParameters();
Global.WriteSeparateList(SeparateFileList.ProcessStages.ParametersSet);
WindowState = FormWindowState.Normal;
DialogResult = DialogResult.OK;
}
private void SetListParameters()
{
//this version just sets all the global values to apply after conversion is
//complete.
Global.UnRippedFiles.NumParts = (int) nudFiles.Value;
Global.UnRippedFiles.RegularMins = (double)nudRegMins.Value;
if (cboFileInterval.SelectedItem != null)
{
var fi = (TrackOrFileInterval)cboFileInterval.SelectedItem;
Global.Options.TrackSpacing = fi.Interval;
Global.UnRippedFiles.TrackSpacing = fi.Interval;
}
if (String.IsNullOrEmpty(Global.UnRippedFiles.BookImage))
{
Global.UnRippedFiles.BookImage = Application.StartupPath + @"\" + "defaultimage.jpg";
}
if (radChapsByTime.Checked)
{
Global.UnRippedFiles.ChapterType = Global.ChapterTypes.ByTime;
}
if (radChapsByFile.Checked)
{
Global.UnRippedFiles.ChapterType = Global.ChapterTypes.BySourceFile;
}
if (radChapsNone.Checked)
{
Global.UnRippedFiles.ChapterType = Global.ChapterTypes.None;
Global.UnRippedFiles.BookImage = "";
}
Global.Options.ChapterType = Global.UnRippedFiles.ChapterType;
Global.Options.Save();
}
private void frmSeparateMergeChap_Load(object sender, EventArgs e)
{
InitImage();
InitIntervals();
InitEncoder();
InitChapterTypes();
}
private void InitChapterTypes()
{
switch(Global.Options.ChapterType)
{
case Global.ChapterTypes.None:
radChapsNone.Checked = true;
break;
case Global.ChapterTypes.ByTime:
radChapsByTime.Checked = true;
break;
case Global.ChapterTypes.BySourceFile:
radChapsByFile.Checked = true;
break;
case Global.ChapterTypes.ByTimeWithDisc: //invalid choice for separate files
radChapsByFile.Checked = true;
break;
}
}
private void InitEncoder()
{
if (Global.Options.Encoder == Global.EncodeToTypes.MP3)
{
labchap7prompt.Text = Resources.MP3_files_can_t_have_chapter_stops;
panel1.Hide();
}
else
{
labchap7prompt.Text = Resources.Audiobooks_on_the_iPod_can_have_chapter_stops;
panel1.Show();
}
}
private void InitIntervals()
{
for (int n = 1; n <= 30; n++)
{
cboFileInterval.Items.Add(new TrackOrFileInterval(n));
}
if (Global.Options.TrackSpacing >= 0 && Global.Options.TrackSpacing <= 30)
cboFileInterval.SelectedIndex = Global.Options.TrackSpacing - 1;
if (Global.Options.DefaultInterval > 0)
nudRegMins.Value = Global.Options.DefaultInterval;
}
private void InitImage()
{
if (File.Exists(Global.Options.DefaultImage))
{
picBookImage.Load(Global.Options.DefaultImage);
}
else
{
picBookImage.Load(Application.StartupPath + @"\" + "defaultimage.jpg");
Global.Options.DefaultImage = Application.StartupPath + @"\" + "defaultimage.jpg";
}
if (String.IsNullOrEmpty(Global.UnRippedFiles.BookImage))
{
Global.UnRippedFiles.BookImage = Global.Options.DefaultImage;
}
SetBookImage();
}
private void butChoosePic_Click(object sender, EventArgs e)
{
dlgOpenImage.FileName = "";
if (dlgOpenImage.ShowDialog() == DialogResult.OK)
{
Global.UnRippedFiles.BookImage = dlgOpenImage.FileName;
SetBookImage();
}
}
private void radChapsByFile_CheckedChanged(object sender, EventArgs e)
{
}
private void frmSeparateMergeChap_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void frmSeparateMergeChap_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
if (files.Length == 0)
return;
string graphicexts = ".gif.png.jpg.bmp";
try
{
string ext = Global.GetExtension(files[0]);
if (graphicexts.IndexOf(ext) == -1)
return; //not a graphic file
Image imgsmall = Global.MakeSmallImage(files[0]);
Global.UnRippedFiles.BookImage = files[0];
picBookImage.Image = imgsmall;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Resources.Unable_to_load_image_exclaim);
}
}
private void nudFiles_Validating(object sender, CancelEventArgs e)
{
FileSizeChecks check = CheckFileSize((int)nudFiles.Value);
if (check == FileSizeChecks.Critical)
{
DialogResult result = MessageBox.Show(Resources.Files_will_be_too_big_to_play_at_the_current_sampling_rate + "\r\n\r\n" + Resources.Ignore_this_query,
Resources.Critical_exclaim,
MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if (result == DialogResult.No)
{
e.Cancel = true;
nudFiles.Focus();
}
return;
}
if (check != FileSizeChecks.OK)
{
DialogResult result = MessageBox.Show(
Resources.Files_will_be_larger_than_recommended_size_and_or_duration + "\r\n\r\n" + Resources.Ignore_this_query,
Resources.Warning_exclaim,
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.No)
{
e.Cancel = true;
nudFiles.Focus();
}
}
}
private enum FileSizeChecks
{
OK,
BiggerThanOptionDuration,
BiggerThanOptionSize,
Critical
}
private FileSizeChecks CheckFileSize(int NumFiles)
{
if ((Global.UnRippedFiles != null) && (Global.UnRippedFiles.Count > 0))
{
double totsize = Global.UnRippedFiles.TotalSize() / 1E6; //this should give MB
TimeSpan totduration = Global.UnRippedFiles.TotalDuration();
totsize = totsize / (double)NumFiles;
double newticks = (double)totduration.Ticks / (double)NumFiles;
long longticks = Convert.ToInt64(newticks);
totduration = new TimeSpan(longticks);
if (totduration > MaxDurationForSampleRate)
{
return FileSizeChecks.Critical;
}
if (totduration.TotalHours > (double)Global.Options.MaxHours)
{
return FileSizeChecks.BiggerThanOptionDuration;
}
if
(totsize > (double)Global.Options.MaxSize)
{
return FileSizeChecks.BiggerThanOptionSize;
}
return FileSizeChecks.OK;
}
return FileSizeChecks.OK;
}
private void butBatch_Click(object sender, EventArgs e)
{
string BatchFolder = Global.GetBatchFolder();
dlgBatchFile.InitialDirectory = BatchFolder;
bool Done = false;
while (!Done)
{
if (dlgBatchFile.ShowDialog() == DialogResult.OK)
{
var fname = dlgBatchFile.FileName;
Done = SaveBatch(fname);
if (Done)
{
Global.ProcessMode = Global.ProcessModes.AddedToBatch;
WindowState = FormWindowState.Normal;
DialogResult = DialogResult.OK;
}
}
else
{
Done = true;
}
}
}
private bool SaveBatch(string fname)
{
if (File.Exists(fname))
{
try
{
Global.CurrentBatch = (BatchList)Global.FromXmlFile(fname, typeof(BatchList));
}
catch (Exception ex)
{
MessageBox.Show(Resources.Unable_to_open_batch_file + "\r\n" + ex.Message, Resources.Error_exclaim);
Global.CurrentBatch = null;
}
if (Global.CurrentBatch != null)
{
var sb = new StringBuilder();
foreach (SeparateFileList fl in Global.CurrentBatch.Lists)
{
sb.AppendLine(fl.ToString());
}
if (MessageBox.Show(
Resources.This_batchfile_already_contains_the_following_projects + ":" + "\r\n\r\n" +
sb.ToString()
+ "\r\n"
+ Resources.Do_you_want_to_continue_query + "\r\n\r\n" +
Resources.If_you_say_YES__this_project_will_be_added_to_the_existing_batchfile,
Resources.Add_to_existing_batch_query, MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
DialogResult.No)
{
return false;
}
}
else
{
MessageBox.Show(Resources.Unable_to_open_batch_file, Resources.Error_exclaim);
return false;
}
}
else
{
Global.CurrentBatch = new BatchList();
}
SetListParameters();
Global.CurrentBatch.Lists.Add(Global.UnRippedFiles);
try
{
Global.ToXmlFile(fname, Global.CurrentBatch, false);
Global.DeleteSeparateList(); //need to get rid of temporary file if it's going into a batch.
MessageBox.Show(Resources.Project_added_to_batch, Resources.Success_exclaim);
return true;
}
catch (Exception ex)
{
MessageBox.Show(Resources.Unable_to_save_batch + ":" + ex.Message, Resources.Error_exclaim);
return false;
}
}
private void butCancel_Click(object sender, EventArgs e)
{
Global.WriteSeparateList(SeparateFileList.ProcessStages.MetaDataAdded); //if hit cancel, we're one step behind.
//report stuff about resume
WindowState = FormWindowState.Normal;
MessageBox.Show(Resources.Cancelled + " " + Resources.However_you_can_resume_converting_this_book_at_a_later_time_if_you_wish,
Resources.Cancelled);
DialogResult = DialogResult.Cancel;
}
}
}