File: SaveBlocksPlugin.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (769 lines) | stat: -rw-r--r-- 29,759 bytes parent folder | download
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
// SPDX-FileCopyrightText: 2007 Thomas Eschenbacher <Thomas.Eschenbacher@gmx.de>
// SPDX-FileCopyrightText: 2024 Mark Penner <mrp@markpenner.space>
// SPDX-License-Identifier: GPL-2.0-or-later
/***************************************************************************
   SaveBlocksPlugin.cpp  -  Plugin for saving blocks between labels
                             -------------------
    begin                : Thu Mar 01 2007
    copyright            : (C) 2007 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

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

#include "config.h"

#include <errno.h>

#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QPointer>
#include <QRegularExpression>
#include <QStringList>

#include <KLocalizedString>

#include "libkwave/CodecManager.h"
#include "libkwave/Encoder.h"
#include "libkwave/FileInfo.h"
#include "libkwave/Label.h"
#include "libkwave/LabelList.h"
#include "libkwave/MessageBox.h"
#include "libkwave/MetaDataList.h"
#include "libkwave/Parser.h"
#include "libkwave/SignalManager.h"
#include "libkwave/String.h"
#include "libkwave/Utils.h"

#include "SaveBlocksDialog.h"
#include "SaveBlocksPlugin.h"

using namespace Qt::StringLiterals;

KWAVE_PLUGIN(saveblocks, SaveBlocksPlugin)

//***************************************************************************
Kwave::SaveBlocksPlugin::SaveBlocksPlugin(QObject *parent,
                                          const QVariantList &args)
    :Kwave::Plugin(parent, args),
     m_url(), m_pattern(), m_numbering_mode(CONTINUE),
     m_selection_only(true), m_block_info()
{
}

//***************************************************************************
Kwave::SaveBlocksPlugin::~SaveBlocksPlugin()
{
}

//***************************************************************************
QStringList *Kwave::SaveBlocksPlugin::setup(QStringList &previous_params)
{
    // try to interpret the previous parameters
    interpreteParameters(previous_params);

    // create the setup dialog
    sample_index_t selection_left  = 0;
    sample_index_t selection_right = 0;
    selection(nullptr, &selection_left, &selection_right, false);

    // enable the "selection only" checkbox only if there is something
    // selected but not everything
    bool selected_something = (selection_left != selection_right);
    bool selected_all = ((selection_left == 0) &&
                         (selection_right + 1 >= signalLength()));
    bool enable_selection_only = selected_something && !selected_all;

    QString base = findBase(m_url.path(), m_pattern);
    scanBlocksToSave(base, m_selection_only && enable_selection_only);

    QUrl signalname = Kwave::URLfromUserInput(signalName());

    Kwave::SaveBlocksDialog *dialog = new Kwave::SaveBlocksDialog(
            parentWidget(),
            signalname,
            m_pattern,
            m_numbering_mode,
            m_selection_only,
            enable_selection_only
        );
        if (!dialog) return nullptr;

    // connect the signals/slots from the plugin and the dialog
    connect(dialog, &SaveBlocksDialog::sigSelectionChanged,
        this, &SaveBlocksPlugin::updateExample);
    connect(this, &SaveBlocksPlugin::sigNewExample,
        dialog, &SaveBlocksDialog::setNewExample);

    dialog->setWindowTitle(description());
    dialog->emitUpdate();
    if ((dialog->exec() != QDialog::Accepted) || !dialog) {
        return nullptr;
    }

    QUrl url = dialog->selectedUrl();
    if (url.isEmpty()) {
        return nullptr;
    }
    QString name = url.path();

    QString filename = signalname.fileName();
    QFileInfo path(filename);

    // replace the current extension with the selected extension
    QString old_ext = path.suffix();
    if (old_ext.length()) filename.remove(old_ext);
    QString ext = Kwave::Parser::escape(dialog->extension());
    filename += ext.mid(1);
    name += u"/"_s + filename;

    name = Kwave::Parser::escape(name);
    QString pattern = Kwave::Parser::escape(dialog->pattern());
    int mode = static_cast<int>(dialog->numberingMode());
    bool selection_only = (enable_selection_only) ?
        dialog->selectionOnly() : m_selection_only;

    QStringList *list = new(std::nothrow) QStringList();
    Q_ASSERT(list);
    if (!list) {
        return nullptr;
    }
    *list << name;
    *list << pattern;
    *list << QString::number(mode);
    *list << QString::number(selection_only);

    emitCommand(u"plugin:execute(saveblocks,"_s +
        name + u","_s + pattern + u","_s +
        QString::number(mode) + u","_s +
        QString::number(selection_only) + u")"_s
    );

    return list;
}

//***************************************************************************
QString Kwave::SaveBlocksPlugin::createDisplayList(
    const QStringList &list,
    unsigned int max_entries) const
{
    if (!max_entries || list.isEmpty()) return QString();

    QString retval;
    unsigned int count = 0;

    foreach (const QString &entry, list) {
        if (count == 0) // first entry
            retval = _("<br><br>");
        if (count < max_entries)
            retval += entry + _("<br>");
        else if (count == max_entries)
            retval += i18n("...") + _("<br>");

        if (++count > max_entries)
            break;
    }

    return retval;
}

//***************************************************************************
int Kwave::SaveBlocksPlugin::start(QStringList &params)
{
    qDebug("SaveBlocksPlugin::start()");

    // interpret the parameters
    int result = interpreteParameters(params);
    if (result) return result;

    QString filename = m_url.path();
    QFileInfo file(filename);
    QString path = file.absolutePath();
    QString ext  = file.suffix();
    QString base = findBase(filename, m_pattern);
    QByteArray sep("/");

    // determine the selection settings
    sample_index_t selection_left  = 0;
    sample_index_t selection_right = 0;
    selection(nullptr, &selection_left, &selection_right, false);

    bool selected_something = (selection_left != selection_right);
    bool selected_all = ( (selection_left == 0) &&
                          ((selection_right + 1) >= signalLength()) );
    bool enable_selection_only = selected_something && !selected_all;
    bool selection_only = enable_selection_only && m_selection_only;
    if (!selection_only) {
        selection_left  = 0;
        selection_right = signalLength() - 1;
    }

    // get the index range
    scanBlocksToSave(base, selection_only);
    unsigned int count = static_cast<unsigned int>(m_block_info.count());
    unsigned int first = firstIndex(path, base, ext, m_pattern,
                                    m_numbering_mode, count);

//     qDebug("m_url            = '%s'", m_url.toDisplayString().toLocal8Bit().data());
//     qDebug("m_pattern        = '%s'", m_pattern.toLocal8Bit().data());
//     qDebug("m_numbering_mode = %d", (int)m_numbering_mode);
//     qDebug("selection_only   = %d", selection_only);
//     qDebug("indices          = %u...%u (count=%u)", first,
//             first + count - 1, count);

    // remember the original file info and determine the list of unsupported
    // properties, we need that later to avoid that the signal manager
    // complains on saving each and every block, again and again...
    const Kwave::FileInfo orig_file_info(signalManager().metaData());
    Kwave::FileInfo file_info(orig_file_info);
    QList<Kwave::FileProperty> unsupported_properties;
    {
        QString mimetype = Kwave::CodecManager::mimeTypeOf(m_url);
        Kwave::Encoder *encoder = Kwave::CodecManager::encoder(mimetype);
        if (encoder) {
            unsupported_properties = encoder->unsupportedProperties(
                file_info.properties().keys());
            delete encoder;
        }
    }

    // iterate over all blocks to check for overwritten files and missing dirs
    QStringList  overwritten_files;
    QStringList  missing_dirs;
    for (unsigned int i = first; i < (first + count); i++) {
        QString name = createFileName(base, ext, m_pattern, i, count,
                                      first + count - 1);
        QString display_name = Kwave::Parser::unescape(name);

        // split the name into directory and file name
        name = QString::fromLatin1(QUrl::toPercentEncoding(display_name, sep));
        QUrl url = m_url.adjusted(QUrl::RemoveFilename);
        url.setPath(url.path(QUrl::FullyEncoded) + name, QUrl::StrictMode);

        QString p = url.path();
        QFileInfo fi(p);

        // check for potentially overwritten file
        if (fi.exists())
            overwritten_files += Kwave::Parser::unescape(display_name);

        // check for missing subdirectory
        if (!fi.dir().exists()) {
            QString missing_dir = fi.absolutePath();
            if (!missing_dirs.contains(missing_dir))
                missing_dirs += missing_dir;
        }
    }

    // inform about overwritten files
    if (!overwritten_files.isEmpty()) {
        // ask the user for confirmation if he really wants to overwrite...
        if (Kwave::MessageBox::warningYesNo(parentWidget(),
            _("<html>") +
            i18n("This would overwrite the following file(s): %1" \
            "Do you really want to continue?",
            createDisplayList(overwritten_files, 5)) +
            _("</html>") ) != KMessageBox::PrimaryAction)
        {
            return -1;
        }
    }

    // handle missing directories
    if (!missing_dirs.isEmpty()) {
        // ask the user if he wants to continue and create the directory
        if (Kwave::MessageBox::warningContinueCancel(parentWidget(),
            i18n("The following directories do not exist: %1"
                 "Do you want to create them and continue?",
                 createDisplayList(missing_dirs, 5)),
            QString(),
            QString(),
            QString(),
            _("saveblocks_create_missing_dirs")
            ) != KMessageBox::Continue)
        {
            return -1;
        }

        // create all missing directories
        QUrl base_url = m_url.adjusted(QUrl::RemoveFilename);
        foreach (const QString &missing, missing_dirs) {
            QUrl url(base_url);
            url.setPath(
                QString::fromLatin1(QUrl::toPercentEncoding(missing)),
                QUrl::StrictMode
            );
            QString p = url.path();
            QDir dir;
            if (!dir.mkpath(p))
                qWarning("creating path '%s' failed", DBG(p));
        }
    }

    // save the current selection, we have to restore it afterwards!
    sample_index_t saved_selection_left  = 0;
    sample_index_t saved_selection_right = 0;
    selection(nullptr, &saved_selection_left, &saved_selection_right, false);

    // now we can loop over all blocks and save them
    sample_index_t block_start;
    sample_index_t block_end = 0;
    Kwave::LabelList labels(signalManager().metaData());
    Kwave::LabelListIterator it(labels);
    Kwave::Label label = it.hasNext() ? it.next() : Kwave::Label();

    for (unsigned int index = first;;) {
        block_start = block_end;
        block_end   = (label.isNull()) ? signalLength() : label.pos();

        if ((selection_left < block_end) && (selection_right > block_start)) {
            // found a block to save...
            Q_ASSERT(index < first + count);

            sample_index_t left  = block_start;
            sample_index_t right = block_end - 1;
            if (left  < selection_left)  left  = selection_left;
            if (right > selection_right) right = selection_right;
            Q_ASSERT(right > left);
            if (right <= left) break; // zero-length ?

            // select the range of samples
            selectRange(left, right - left + 1);

            // determine the filename
            QString name = createFileName(base, ext, m_pattern, index, count,
                                          first + count - 1);
            name = Kwave::Parser::unescape(name);
            // use URL encoding for the filename
            name = QString::fromLatin1(QUrl::toPercentEncoding(name, sep));
            QUrl url = m_url.adjusted(QUrl::RemoveFilename);
            url.setPath(url.path(QUrl::FullyEncoded) + name, QUrl::StrictMode);

            // enter the title of the block into the meta data if supported
            if (!unsupported_properties.contains(INF_NAME)) {
                QString title = orig_file_info.get(INF_NAME).toString();
                int idx = index - first;
                if ((idx >= 0) && (idx < m_block_info.count())) {
                    QString block_title = m_block_info[idx].m_title;
                    if (block_title.length())
                        title = title + _(", ") + block_title;
                }
                file_info.set(INF_NAME, QVariant(title));
                signalManager().metaData().replace(
                    Kwave::MetaDataList(file_info));
            }

            qDebug("saving %9lu...%9lu -> '%s'",
                   static_cast<unsigned long int>(left),
                   static_cast<unsigned long int>(right),
                   DBG(url.toDisplayString()));
            if (signalManager().save(url, true) < 0)
                break;

            // if there were unsupported properties, the user might have been
            // asked whether it is ok to continue or not. If he answered with
            // "Cancel", we do not reach this point, otherwise we can continue
            // and prevent any further annoying questions by removing all
            // unsupported file info before the next run...
            if ((index == first) && !unsupported_properties.isEmpty()) {
                foreach (const Kwave::FileProperty &p, unsupported_properties) {
                    file_info.set(p, QVariant());
                }
                signalManager().metaData().replace(
                    Kwave::MetaDataList(file_info));
            }

            // increment the index for the next filename
            index++;
        }
        if (label.isNull()) break;
        label = (it.hasNext()) ? it.next() : Kwave::Label();
    }

    // restore the original file info
    signalManager().metaData().replace(
        Kwave::MetaDataList(orig_file_info));

    // restore the previous selection
    selectRange(saved_selection_left,
        (saved_selection_left != saved_selection_right) ?
        (saved_selection_right - saved_selection_left + 1) : 0);

    return result;
}

//***************************************************************************
int Kwave::SaveBlocksPlugin::interpreteParameters(QStringList &params)
{
    bool ok;
    QString param;

    // evaluate the parameter list
    if (params.count() != 4) {
        return -EINVAL;
    }

    // the selected URL
    m_url = Kwave::URLfromUserInput(Kwave::Parser::unescape(params[0]));
    if (!m_url.isValid()) return -EINVAL;

    // filename pattern
    m_pattern = Kwave::Parser::unescape(params[1]);
    if (!m_pattern.length()) return -EINVAL;

    // numbering mode
    param = params[2];
    int mode = param.toInt(&ok);
    Q_ASSERT(ok);
    if (!ok) return -EINVAL;
    if ((mode != CONTINUE) &&
        (mode != START_AT_ONE)) return -EINVAL;
    m_numbering_mode = static_cast<numbering_mode_t>(mode);

    // flag: save only the selection
    param = params[3];
    m_selection_only = (param.toUInt(&ok) != 0);
    Q_ASSERT(ok);
    if (!ok) return -EINVAL;

    return 0;
}

//***************************************************************************
void Kwave::SaveBlocksPlugin::scanBlocksToSave(const QString &base,
                                               bool selection_only)
{
    sample_index_t selection_left, selection_right;

    sample_index_t block_start;
    sample_index_t block_end = 0;
    QString        block_title;
    Kwave::LabelList labels(signalManager().metaData());
    Kwave::LabelListIterator it(labels);
    Kwave::Label label = (it.hasNext()) ? it.next() : Kwave::Label();

    if (selection_only) {
        selection(nullptr, &selection_left, &selection_right, true);
    } else {
        selection_left  = 0;
        selection_right = signalLength() - 1;
    }

    // get the title of the whole file, in case that a block does not have
    // an own title
    FileInfo info(signalManager().metaData());
    QString file_title = info.get(INF_NAME).toString();

    // fallback: if there is no INF_NAME either, fall back to the file
    //           name as last resort
    if (!file_title.length()) file_title = base;

    m_block_info.clear();
    QString prev_title;
    for (;;) {
        block_start = block_end;
        block_end   = (label.isNull()) ? signalLength() : label.pos();
        block_title = prev_title;
        prev_title  = (label.isNull()) ? file_title     : label.name();

        if ((block_end > selection_left) && (block_start <= selection_right)) {
            BlockInfo block;
            block.m_start  = block_start;
            block.m_length = block_end - block_start;
            block.m_title  = block_title;
            if (!block.m_title.length()) block.m_title = file_title;
            m_block_info.append(block);
        }

        if (label.isNull()) break;
        label = (it.hasNext()) ? it.next() : Kwave::Label();
    }
}

//***************************************************************************
QString Kwave::SaveBlocksPlugin::createFileName(const QString &base,
    const QString &ext, const QString &pattern,
    unsigned int index, int count, int total)
{
    QString p = QRegularExpression::escape(pattern);
    QString nr;

    // format the "index" parameter
    QRegularExpression rx_nr(_("(\\\\\\[\\\\%(\\d*)nr\\\\\\])"),
                             QRegularExpression::CaseInsensitiveOption);
    QRegularExpressionMatchIterator i = rx_nr.globalMatch(p);
    while (i.hasNext()) {
        QRegularExpressionMatch match = i.next();
        QString format = match.captured(1);
        format = format.mid(3, format.length() - 7);
        QString ex = _("(\\\\\\[\\\\") + format + _("nr\\\\\\])");
        QRegularExpression rx(ex, QRegularExpression::CaseInsensitiveOption);
        format += _("u");
        p.replace(rx, nr.asprintf(format.toLatin1().constData(), index));
    }

    // format the "count" parameter
    QRegularExpression rx_count(_("(\\\\\\[\\\\%\\d*count\\\\\\])"),
                                QRegularExpression::CaseInsensitiveOption);
    i = rx_count.globalMatch(p);
    while (i.hasNext()) {
        QRegularExpressionMatch match = i.next();
        if (count >= 0) {
            QString format = match.captured(1);
            format = format.mid(3, format.length() - 10);
            QString ex = _("(\\\\\\[\\\\") + format + _("count\\\\\\])");
            QRegularExpression rx(ex,
                QRegularExpression::CaseInsensitiveOption);
            format += _("u");
            p.replace(rx, nr.asprintf(format.toLatin1().constData(), count));
        } else {
            p.replace(rx_count, _("(\\d+)"));
        }
    }

    // format the "total" parameter
    QRegularExpression rx_total(_("(\\\\\\[\\\\%\\d*total\\\\\\])"),
                                QRegularExpression::CaseInsensitiveOption);
    i = rx_total.globalMatch(p);
    while (i.hasNext()) {
        QRegularExpressionMatch match = i.next();
        if (total >= 0) {
            QString format = match.captured(1);
            format = format.mid(3, format.length() - 10);
            QString ex = _("(\\\\\\[\\\\") + format + _("total\\\\\\])");
            QRegularExpression rx(ex,
                                  QRegularExpression::CaseInsensitiveOption);
            format += _("u");
            p.replace(rx, nr.asprintf(format.toLatin1().constData(), total));
        } else {
            p.replace(rx_total, _("(\\d+)"));
        }
    }

    // format the "filename" parameter
    QRegularExpression rx_filename(_("\\\\\\[\\\\%filename\\\\\\]"),
                                   QRegularExpression::CaseInsensitiveOption);
    if (p.indexOf(rx_filename) >= 0) {
        p.replace(rx_filename, QRegularExpression::escape(base));
    }

    // support for file info
    QRegularExpression rx_fileinfo(
        _("\\\\\\[\\\\%(\\d*)fileinfo\\\\\\{([\\w\\s]+)\\\\\\}\\\\\\]"),
        QRegularExpression::CaseInsensitiveOption
    );
    Kwave::FileInfo info(signalManager().metaData());
    i = rx_fileinfo.globalMatch(p);
    while (i.hasNext()) {
        QRegularExpressionMatch match = i.next();
        const QString format = match.captured(1);
        const QString id     = match.captured(2);
        QString value;
        FileProperty property = info.fromName(id);
        if (property != Kwave::INF_UNKNOWN) {
            QVariant val = info.get(property);
            if (!val.isNull()) {
                // we have a property value
                value = val.toString();

                // check for format (desired minimum string length)
                bool ok  = false;
                int  len = format.toUInt(&ok);
                if ((len > 0) && ok) {
                    Kwave::FileInfo::Flags flags = info.flags(property);
                    if (flags & Kwave::FileInfo::FP_FORMAT_NUMERIC) {
                        // numeric format, pad with leading zeros or spaces
                        QString pad = (format.startsWith(QLatin1Char('0'))) ?
                            _("0") : _(" ");
                        while (value.length() < len)
                            value = pad + value;
                    } else {
                        // string format, pad with trailing spaces
                        while (value.length() < len)
                            value = value + _(" ");
                    }
                }
                value = Kwave::Parser::escapeForFileName(value);
            }
        }

        QString ex(_("(\\\\\\[\\\\%") + format + _("fileinfo\\\\\\{") + id +
                   _("\\\\\\}\\\\\\])"));
        QRegularExpression rx(ex, QRegularExpression::CaseInsensitiveOption);
        p.replace(rx, value);
    }

    // format the "title" parameter
    QRegularExpression rx_title(_("\\\\\\[\\\\%title\\\\\\]"),
                                QRegularExpression::CaseInsensitiveOption);
    if (p.indexOf(rx_title) >= 0) {
        QString title;
        int idx = (index - 1) - (total - count);
        if ((idx >= 0) && (idx < m_block_info.count()))
            title = m_block_info[idx].m_title;
        if (title.length()) {
            title = Kwave::Parser::escapeForFileName(title);
            p.replace(rx_title, title);
        }
    }

    if (ext.length()) p += _(".") + ext;

    // sanitize the filename/path, make sure that there are no spaces
    // before and after all path separators
    QString sep = _("/");
    QRegularExpression rx_sep(_("\\s*") + sep + _("\\s*"));
    p.replace(rx_sep, sep);

    return p;
}

//***************************************************************************
unsigned int Kwave::SaveBlocksPlugin::firstIndex(const QString &path,
    const QString &base, const QString &ext, const QString &pattern,
    Kwave::SaveBlocksPlugin::numbering_mode_t mode, unsigned int count)
{
    unsigned int first = 1;
    switch (mode) {
        case START_AT_ONE:
            first = 1;
            break;
        case CONTINUE: {
            QDir dir(path, _("*"));
            QStringList files;
            files = dir.entryList();
            for (unsigned int i = first; i < (first + count); i++) {
                QString name = createFileName(base, ext, pattern, i, -1, -1);
                QRegularExpression rx(_("^(") + name + _(")$"),
                           QRegularExpression::CaseInsensitiveOption);
                QStringList matches = files.filter(rx);
                if (matches.count() > 0) first = i + 1;
            }
            break;
        }
        DEFAULT_IMPOSSIBLE;
    }

    return first;
}

//***************************************************************************
QString Kwave::SaveBlocksPlugin::findBase(const QString &filename,
                                          const QString &pattern)
{
    QFileInfo file(filename);
    QString name = file.fileName();
    QString base = file.completeBaseName();
    QString ext  = file.suffix();

    // convert the pattern into a regular expression in order to check if
    // the current name already is produced by the current pattern
    // \[%[0-9]?nr\]      -> \d+
    // \[%[0-9]?count\]   -> \d+
    // \[%[0-9]?total\]   -> \d+
    // \[%filename\]      -> base
    // \[%fileinfo\]      -> .
    // \[%title\]         -> .
    QRegularExpression rx_nr(_("\\\\\\[\\\\%\\d*nr\\\\\\]"),
                             QRegularExpression::CaseInsensitiveOption);
    QRegularExpression rx_count(_("\\\\\\[\\\\%\\d*count\\\\\\]"),
                                QRegularExpression::CaseInsensitiveOption);
    QRegularExpression rx_total(_("\\\\\\[\\\\%\\d*total\\\\\\]"),
                                QRegularExpression::CaseInsensitiveOption);
    QRegularExpression rx_filename(_("\\\\\\[\\\\%filename\\\\\\]"),
                                   QRegularExpression::CaseInsensitiveOption);
    QRegularExpression rx_fileinfo(_("\\\\\\[\\\\%fileinfo\\\\\\]"),
                                   QRegularExpression::CaseInsensitiveOption);
    QRegularExpression rx_title(_("\\\\\\[\\\\%title\\\\\\]"),
                                QRegularExpression::CaseInsensitiveOption);

    QString p = QRegularExpression::escape(pattern);
    qsizetype idx_nr       = p.indexOf(rx_nr);
    qsizetype idx_count    = p.indexOf(rx_count);
    qsizetype idx_total    = p.indexOf(rx_total);
    int       idx_filename = static_cast<int>(p.indexOf(rx_filename));
    qsizetype idx_fileinfo = p.indexOf(rx_fileinfo);
    qsizetype idx_title    = p.indexOf(rx_fileinfo);
    p.replace(rx_nr,       _("(\\d+)"));
    p.replace(rx_count,    _("(\\d+)"));
    p.replace(rx_total,    _("(\\d+)"));
    p.replace(rx_filename, _("(.+)"));
    p.replace(rx_fileinfo, _("(.+)"));
    p.replace(rx_title,    _("(.+)"));

    qsizetype max = 0;
    for (qsizetype i = 0; i < pattern.length(); i++) {
        if (idx_nr       == max) max++;
        if (idx_count    == max) max++;
        if (idx_total    == max) max++;
        if (idx_filename == max) max++;
        if (idx_fileinfo == max) max++;
        if (idx_title    == max) max++;

        if (idx_nr       > max) idx_nr--;
        if (idx_count    > max) idx_count--;
        if (idx_total    > max) idx_total--;
        if (idx_filename > max) idx_filename--;
        if (idx_fileinfo > max) idx_fileinfo--;
        if (idx_title    > max) idx_title--;
    }

    if (ext.length()) p += _(".") + ext;
    QRegularExpression rx_current(p, QRegularExpression::CaseInsensitiveOption);
    QRegularExpressionMatch rxm_current;
    if (name.indexOf(rx_current, 0, &rxm_current) >= 0) {
        // filename already produced by this pattern
        base = rxm_current.captured(idx_filename + 1);
    }

    return base;
}

//***************************************************************************
QString Kwave::SaveBlocksPlugin::firstFileName(const QString &filename,
                                               const QString &pattern,
                                               Kwave::SaveBlocksPlugin::numbering_mode_t mode,
                                               const QString &extension,
                                               bool selection_only)
{
    QFileInfo file(filename);
    QString path = file.absolutePath();
    QString ext  = extension.mid(1);
    QString base = findBase(filename, pattern);

    // now we have a new name, base and extension
    // -> find out the numbering, min/max etc...
    scanBlocksToSave(base, selection_only);
    unsigned int count = static_cast<unsigned int>(m_block_info.count());
    unsigned int first = firstIndex(path, base, ext, pattern, mode, count);
    unsigned int total = first + count - 1;

    // create the complete filename, including extension but without path
    return createFileName(base, ext, pattern, first, count, total);
}

//***************************************************************************
void Kwave::SaveBlocksPlugin::updateExample(const QString &filename,
                                            const QString &pattern,
                                            Kwave::SaveBlocksPlugin::numbering_mode_t mode,
                                            const QString &extension,
                                            bool selection_only)
{
    QString example = firstFileName(filename, pattern, mode, extension, selection_only);
    emit sigNewExample(Kwave::Parser::unescape(example));
}

//***************************************************************************
#include "SaveBlocksPlugin.moc"
//***************************************************************************
//***************************************************************************

#include "moc_SaveBlocksPlugin.cpp"