summaryrefslogtreecommitdiff
path: root/FreeFileSync/Source/ui/cfg_grid.h
blob: 7bc8633a0de3bba06073161fa6f679c472864b95 (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
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under    *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0          *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************

#ifndef CONFIG_HISTORY_3248789479826359832
#define CONFIG_HISTORY_3248789479826359832

#include <wx+/grid.h>
#include <wx+/dc.h>
#include <zen/zstring.h>
#include "../return_codes.h"
#include "../afs/concrete.h"


namespace fff
{
struct LastRunStats
{
    AbstractPath logFilePath = getNullPath(); //optional
    time_t syncTime = 0;
    SyncResult syncResult = SyncResult::aborted;
    int     itemsProcessed = 0;
    int64_t bytesProcessed = 0;
    std::chrono::milliseconds totalTime{};
    int errors   = 0;
    int warnings = 0;
};

struct ConfigFileItem
{
    ConfigFileItem() {}
    ConfigFileItem(const Zstring& filePath,
                   const LastRunStats& runStats,
                   wxColor bcol) :
        cfgFilePath(filePath),
        lastRunStats(runStats),
        backColor(bcol) {}

    Zstring cfgFilePath;
    LastRunStats lastRunStats;
    wxColor backColor;
    wxColor backColorPreview; //while the folder picker is shown
};


enum class ColumnTypeCfg
{
    name,
    lastSync,
    lastLog,
};

struct ColAttributesCfg
{
    ColumnTypeCfg type    = ColumnTypeCfg::name;
    int           offset  = 0;
    int           stretch = 0;
    bool          visible = false;
};

inline
std::vector<ColAttributesCfg> getCfgGridDefaultColAttribs()
{
    using namespace zen;
    return
    {
        {ColumnTypeCfg::name,     -fastFromDIP(75) - fastFromDIP(42), 1, true},
        {ColumnTypeCfg::lastSync, fastFromDIP(75), 0, true},
        {ColumnTypeCfg::lastLog,  fastFromDIP(42), 0, true}, //leave some room for the sort direction indicator
    };
}

const ColumnTypeCfg cfgGridLastSortColumnDefault = ColumnTypeCfg::name;

inline
bool getDefaultSortDirection(ColumnTypeCfg colType)
{
    switch (colType)
    {
        case ColumnTypeCfg::name:
            return true;
        case ColumnTypeCfg::lastSync: //actual sort order is "time since last sync"
            return false;
        case ColumnTypeCfg::lastLog:
            return true;
    }
    assert(false);
    return true;
}
//---------------------------------------------------------------------------------------------------------------------
Zstring getLastRunConfigPath();


class ConfigView
{
public:
    ConfigView() {}

    std::vector<ConfigFileItem> get() const;
    void set(const std::vector<ConfigFileItem>& cfgItems);

    void addCfgFiles(const std::vector<Zstring>& filePaths);
    void removeItems(const std::vector<Zstring>& filePaths);
    void renameItem(const Zstring& pathFrom, const Zstring& pathTo);

    void setNotes(const Zstring& filePath, const std::wstring& notes);
    void setLastRunStats(const std::vector<Zstring>& filePaths, const LastRunStats& lastRun);
    void setBackColor(const std::vector<Zstring>& filePaths, const wxColor& col, bool previewOnly = false);

    struct Details
    {
        ConfigFileItem cfgItem;

        Zstring name;
        int lastUseIndex = 0; //support truncating the config list size via last usage, the higher the index the more recent the usage
        bool isLastRunCfg = false; //LastRun.ffs_gui
        std::wstring notes;

        enum ConfigType
        {
            CFG_TYPE_NONE,
            CFG_TYPE_GUI,
            CFG_TYPE_BATCH,
        } cfgType = CFG_TYPE_NONE;
    };

    const Details* getItem(size_t row) const;
    std::pair<const ConfigView::Details*, size_t /*row*/> getItem(const Zstring& filePath) const;

    size_t getRowCount() const { assert(cfgList_.size() == cfgListView_.size()); return cfgListView_.size(); }

    void setSortDirection(ColumnTypeCfg colType, bool ascending);
    std::pair<ColumnTypeCfg, bool> getSortDirection() { return {sortColumn_, sortAscending_}; }

private:
    ConfigView           (const ConfigView&) = delete;
    ConfigView& operator=(const ConfigView&) = delete;

    void addCfgFilesImpl(const std::vector<Zstring>& filePaths);

    void sortListView();
    template <bool ascending> void sortListViewImpl();

    const Zstring lastRunConfigPath_ = getLastRunConfigPath(); //let's not use another static...

    using CfgFileList = std::map<Zstring /*file path*/, Details, zen::LessNativePath>;

    CfgFileList cfgList_;
    std::vector<CfgFileList::iterator> cfgListView_; //sorted view on cfgList_

    ColumnTypeCfg sortColumn_ = cfgGridLastSortColumnDefault;
    bool sortAscending_       = getDefaultSortDirection(cfgGridLastSortColumnDefault);
};


namespace cfggrid
{
void init(zen::Grid& grid);
ConfigView& getDataView(zen::Grid& grid); //grid.Refresh() after making changes!

void addAndSelect(zen::Grid& grid, const std::vector<Zstring>& filePaths, bool scrollToSelection);

int  getSyncOverdueDays(zen::Grid& grid);
void setSyncOverdueDays(zen::Grid& grid, int syncOverdueDays);
}
}

#endif //CONFIG_HISTORY_3248789479826359832
bgstack15