summaryrefslogtreecommitdiff
path: root/ui/util.cpp
blob: 853bd91b1ef1e8e649fbe17087437bef0586f5f0 (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
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
#include "util.h"
#include <wx/scrolwin.h>
#include <wx/textctrl.h>
#include <wx/combobox.h>
#include <wx/filepicker.h>
#include "../shared/globalFunctions.h"
#include "../shared/localization.h"
#include "../shared/fileHandling.h"
#include "../shared/stringConv.h"
#include <stdexcept>
#include "../shared/systemFunctions.h"

#ifdef FFS_WIN
#include <wx/msw/wrapwin.h> //includes "windows.h"
#endif


wxString FreeFileSync::formatFilesizeToShortString(const wxLongLong& filesize)
{
    return FreeFileSync::formatFilesizeToShortString(filesize.ToDouble());
}


wxString FreeFileSync::formatFilesizeToShortString(const wxULongLong& filesize)
{
    return FreeFileSync::formatFilesizeToShortString(filesize.ToDouble());
};


wxString FreeFileSync::formatFilesizeToShortString(const double filesize)
{
    if (filesize < 0)
        return _("Error");

    if (filesize <= 999)
        return wxString::Format(wxT("%i"), static_cast<int>(filesize)) + _(" Byte"); //no decimal places in case of bytes

    double nrOfBytes = filesize;

    nrOfBytes /= 1024;
    wxString unit = _(" kB");
    if (nrOfBytes > 999)
    {
        nrOfBytes /= 1024;
        unit = _(" MB");
        if (nrOfBytes > 999)
        {
            nrOfBytes /= 1024;
            unit = _(" GB");
            if (nrOfBytes > 999)
            {
                nrOfBytes /= 1024;
                unit = _(" TB");
                if (nrOfBytes > 999)
                {
                    nrOfBytes /= 1024;
                    unit = _(" PB");
                }
            }
        }
    }

    //print just three significant digits: 0,01 | 0,11 | 1,11 | 11,1 | 111

    const unsigned int leadDigitCount = globalFunctions::getDigitCount(static_cast<unsigned int>(nrOfBytes)); //number of digits before decimal point
    if (leadDigitCount == 0 || leadDigitCount > 3)
        return _("Error");

    if (leadDigitCount == 3)
        return wxString::Format(wxT("%i"), static_cast<int>(nrOfBytes)) + unit;
    else if (leadDigitCount == 2)
    {
        wxString output = wxString::Format(wxT("%i"), static_cast<int>(nrOfBytes * 10));
        output.insert(leadDigitCount, FreeFileSync::DECIMAL_POINT);
        return output + unit;
    }
    else //leadDigitCount == 1
    {
        wxString output = wxString::Format(wxT("%03i"), static_cast<int>(nrOfBytes * 100));
        output.insert(leadDigitCount, FreeFileSync::DECIMAL_POINT);
        return output + unit;
    }

    //return wxString::Format(wxT("%.*f"), 3 - leadDigitCount, nrOfBytes) + unit;
}


wxString FreeFileSync::fromatPercentage(const wxLongLong& dividend, const wxLongLong& divisor)
{
    const double ratio = dividend.ToDouble() * 100 / divisor.ToDouble();
    wxString output = _("%x Percent");
    output.Replace(wxT("%x"), wxString::Format(wxT("%3.2f"), ratio), false);
    return output;
}



wxString FreeFileSync::includeNumberSeparator(const wxString& number)
{
    wxString output(number);
    for (int i = output.size() - 3; i > 0; i -= 3)
        output.insert(i,  FreeFileSync::THOUSANDS_SEPARATOR);

    return output;
}


template <class T>
void setDirectoryNameImpl(const wxString& dirname, T* txtCtrl, wxDirPickerCtrl* dirPicker)
{
    using namespace FreeFileSync;

    txtCtrl->SetValue(dirname);
    const Zstring leftDirFormatted = FreeFileSync::getFormattedDirectoryName(wxToZ(dirname));
    if (FreeFileSync::dirExists(leftDirFormatted))
        dirPicker->SetPath(zToWx(leftDirFormatted));
}


void FreeFileSync::setDirectoryName(const wxString& dirname, wxTextCtrl* txtCtrl, wxDirPickerCtrl* dirPicker)
{
    setDirectoryNameImpl(dirname, txtCtrl, dirPicker);
}


void FreeFileSync::setDirectoryName(const wxString& dirname, wxComboBox* txtCtrl, wxDirPickerCtrl* dirPicker)
{
    txtCtrl->SetSelection(wxNOT_FOUND);
    setDirectoryNameImpl(dirname, txtCtrl, dirPicker);
}


void FreeFileSync::scrollToBottom(wxScrolledWindow* scrWindow)
{
    int height = 0;
    scrWindow->GetClientSize(NULL, &height);

    int pixelPerLine = 0;
    scrWindow->GetScrollPixelsPerUnit(NULL, &pixelPerLine);

    if (height > 0 && pixelPerLine > 0)
    {
        const int scrollLinesTotal    = scrWindow->GetScrollLines(wxVERTICAL);
        const int scrollLinesOnScreen = height / pixelPerLine;
        const int scrollPosBottom     = scrollLinesTotal - scrollLinesOnScreen;

        if (0 <= scrollPosBottom)
            scrWindow->Scroll(0, scrollPosBottom);
    }
}





inline
void writeTwoDigitNumber(unsigned int number, wxString& string)
{
    assert (number < 100);

    string += '0' + number / 10;
    string += '0' + number % 10;
}


inline
void writeFourDigitNumber(unsigned int number, wxString& string)
{
    assert (number < 10000);

    string += '0' + number / 1000;
    number %= 1000;
    string += '0' + number / 100;
    number %= 100;
    string += '0' + number / 10;
    number %= 10;
    string += '0' + number;
}


wxString FreeFileSync::utcTimeToLocalString(const wxLongLong& utcTime, const Zstring& filename)
{
#ifdef FFS_WIN
    //convert ansi C time to FILETIME
    wxLongLong fileTimeLong(utcTime);

    fileTimeLong += wxLongLong(2, 3054539008UL); //timeshift between ansi C time and FILETIME in seconds == 11644473600s
    fileTimeLong *= 10000000;

    FILETIME lastWriteTimeUtc;
    lastWriteTimeUtc.dwLowDateTime  = fileTimeLong.GetLo();             //GetLo() returns unsigned
    lastWriteTimeUtc.dwHighDateTime = unsigned(fileTimeLong.GetHi());   //GetHi() returns signed


    FILETIME localFileTime;
    if (::FileTimeToLocalFileTime( //convert to local time
                &lastWriteTimeUtc, //pointer to UTC file time to convert
                &localFileTime 	   //pointer to converted file time
            ) == 0)
        throw std::runtime_error(std::string((wxString(_("Conversion error:")) + wxT(" FILETIME -> local FILETIME: ") +
                                              wxT("(") + wxULongLong(lastWriteTimeUtc.dwHighDateTime, lastWriteTimeUtc.dwLowDateTime).ToString() + wxT(") ") +
                                              filename.c_str() + wxT("\n\n") + getLastErrorFormatted()).ToAscii()));

    if (localFileTime.dwHighDateTime > 0x7fffffff)
        return _("Error");  //this actually CAN happen if UTC time is just below this border and ::FileTimeToLocalFileTime() adds 2 hours due to DST or whatever!
    //Testcase (UTC): dateHigh = 2147483647 (=0x7fffffff) -> year 30000
    //                dateLow  = 4294967295

    SYSTEMTIME time;
    if (::FileTimeToSystemTime(
                &localFileTime, //pointer to file time to convert
                &time 	        //pointer to structure to receive system time
            ) == 0)
        throw std::runtime_error(std::string((wxString(_("Conversion error:")) + wxT(" local FILETIME -> SYSTEMTIME: ") +
                                              wxT("(") + wxULongLong(localFileTime.dwHighDateTime, localFileTime.dwLowDateTime).ToString() + wxT(") ") +
                                              filename.c_str()  + wxT("\n\n") + getLastErrorFormatted()).ToAscii()));

    //assemble time string (performance optimized)
    wxString formattedTime;
    formattedTime.reserve(20);

    writeFourDigitNumber(time.wYear, formattedTime);
    formattedTime += wxChar('-');
    writeTwoDigitNumber(time.wMonth, formattedTime);
    formattedTime += wxChar('-');
    writeTwoDigitNumber(time.wDay, formattedTime);
    formattedTime += wxChar(' ');
    formattedTime += wxChar(' ');
    writeTwoDigitNumber(time.wHour, formattedTime);
    formattedTime += wxChar(':');
    writeTwoDigitNumber(time.wMinute, formattedTime);
    formattedTime += wxChar(':');
    writeTwoDigitNumber(time.wSecond, formattedTime);

    return formattedTime;

#elif defined FFS_LINUX
    tm* timeinfo;
    const time_t fileTime = utcTime.ToLong();
    timeinfo = localtime(&fileTime); //convert to local time
    char buffer[50];
    strftime(buffer, 50, "%Y-%m-%d  %H:%M:%S", timeinfo);

    return zToWx(buffer);
#endif
}
bgstack15