summaryrefslogtreecommitdiffhomepage
path: root/logic/work.cpp
blob: e0c13ac840fd6bc193f6e9f7e4b2f5eda6e34551 (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
#include "work.hpp"
#include "compat/library-path.hpp"

#include <utility>

#include <QObject>
#include <QMessageBox>
#include <QFileDialog>

QString Work::browse_datalogging_file(main_settings &s)
{
    QString filename = s.tracklogging_filename;
    if (filename.isEmpty())
        filename = OPENTRACK_BASE_PATH;
    /* Sometimes this function freezes the app before opening the dialog.
       Might be related to https://forum.qt.io/topic/49209/qfiledialog-getopenfilename-hangs-in-windows-when-using-the-native-dialog/8
       and be a known problem. Possible solution is to use the QFileDialog::DontUseNativeDialog flag.
       Since the freeze is apparently random, I'm not sure it helped.
    */
    QString newfilename = QFileDialog::getSaveFileName(nullptr,
                                                       tr("Select filename"),
                                                       filename,
                                                       tr("CSV File (*.csv)"),
                                                       nullptr);
    if (!newfilename.isEmpty())
    {
      s.tracklogging_filename = newfilename;
    }
    // dialog likes to mess with current directory
    QDir::setCurrent(OPENTRACK_BASE_PATH);
    return newfilename;
}

std::unique_ptr<TrackLogger> Work::make_logger(main_settings &s)
{
    if (s.tracklogging_enabled)
    {
        QString filename = browse_datalogging_file(s);
        if (filename.isEmpty())
        {
            // The user probably canceled the file dialog. In this case we don't want to do anything.
            return {};
        }
        else
        {
            auto logger = std::make_unique<TrackLoggerCSV>(*s.tracklogging_filename);

            if (!logger->is_open())
            {
                QMessageBox::warning(nullptr,
                    tr("Logging error"),
                    tr("Unable to open file '%1'. Proceeding without logging.").arg(s.tracklogging_filename),
                    QMessageBox::Ok, QMessageBox::NoButton);
            }
            else
                return std::move(logger);
        }
    }

    return std::make_unique<TrackLogger>();
}


Work::Work(Mappings& m, event_handler& ev, QFrame* frame,
           const dylibptr& tracker_, const dylibptr& filter_, const dylibptr& proto_) :
    libs(frame, tracker_, filter_, proto_),
    pipeline_{ m, libs, ev, *logger }
{
    if (!is_ok())
        return;
    reload_shortcuts();
    pipeline_.start();
}

void Work::reload_shortcuts()
{
    sc.reload(keys);
}

bool Work::is_ok() const
{
    return libs.correct;
}

// TODO member dtor order looks fine, check valgrind -sh 20180706
#if 0
Work::~Work()
{
    // order matters, otherwise use-after-free -sh
    //sc = nullptr;
    //pipeline = nullptr;
    //libs = runtime_libraries();
}
#endif