summaryrefslogtreecommitdiff
path: root/lib/dir_name.cpp
blob: 55a4185ce9c59f985f4a661cff2656de33fd65ae (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
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************

#include "dir_name.h"
#include <wx/dnd.h>
#include <wx/window.h>
#include <wx/textctrl.h>
#include <wx/statbox.h>
#include <zen/thread.h>
#include <zen/file_handling.h>
#include "resolve_path.h"
#include <wx+/string_conv.h>
#include "folder_history_box.h"

using namespace zen;


namespace
{
void setDirectoryNameImpl(const wxString& dirname, wxDirPickerCtrl* dirPicker, wxWindow& tooltipWnd, wxStaticBoxSizer* staticBox, size_t timeout)
{
    const wxString dirFormatted = toWx(getFormattedDirectoryName(toZ(dirname)));

    tooltipWnd.SetToolTip(dirFormatted); //wxComboBox bug: the edit control is not updated... http://trac.wxwidgets.org/ticket/12659

    if (staticBox)
    {
        //change static box label only if there is a real difference to what is shown in wxTextCtrl anyway
        wxString dirNormalized = dirname;
        trim(dirNormalized);
        if (!dirNormalized.empty() && !endsWith(dirNormalized, FILE_NAME_SEPARATOR))
            dirNormalized += FILE_NAME_SEPARATOR;

        staticBox->GetStaticBox()->SetLabel(dirNormalized == dirFormatted ? wxString(_("Drag && drop")) : dirFormatted);
    }

    if (dirPicker && !dirFormatted.empty())
    {
        Zstring dir = toZ(dirFormatted); //convert to Zstring first: we don't want to pass wxString by value and risk MT issues!
        auto ft = async([=] { return zen::dirExists(dir); });

        if (ft.timed_wait(boost::posix_time::milliseconds(timeout)) && ft.get()) //potentially slow network access: wait 200ms at most
            dirPicker->SetPath(dirFormatted);
    }
}


void setDirectoryName(const wxString&  dirname,
                      wxTextCtrl*      txtCtrl,
                      wxDirPickerCtrl* dirPicker,
                      wxWindow&        tooltipWnd,
                      wxStaticBoxSizer* staticBox,
                      size_t           timeout = 200) //pointers are optional
{
    if (txtCtrl)
        txtCtrl->ChangeValue(dirname);
    setDirectoryNameImpl(dirname, dirPicker, tooltipWnd, staticBox, timeout);
}


void setDirectoryName(const wxString&   dirname,
                      FolderHistoryBox* comboBox,
                      wxDirPickerCtrl*  dirPicker,
                      wxWindow&         tooltipWnd,
                      wxStaticBoxSizer* staticBox,
                      size_t            timeout = 200) //pointers are optional
{
    if (comboBox)
        comboBox->setValue(dirname);
    setDirectoryNameImpl(dirname, dirPicker, tooltipWnd, staticBox, timeout);
}
}
//##############################################################################################################

template <class NameControl>
DirectoryName<NameControl>::DirectoryName(wxWindow&        dropWindow,
                                          wxDirPickerCtrl& dirPicker,
                                          NameControl&     dirName,
                                          wxStaticBoxSizer* staticBox,
                                          wxWindow*        dropWindow2) :
    dropWindow_(dropWindow),
    dropWindow2_(dropWindow2),
    dirPicker_(dirPicker),
    dirName_(dirName),
    staticBox_(staticBox)
{
    //prepare drag & drop
    setupFileDrop(dropWindow);
    if (dropWindow2)
        setupFileDrop(*dropWindow2);

    //redirect drag & drop event back to this class
    dropWindow.Connect(FFS_DROP_FILE_EVENT, FFSFileDropEventHandler(DirectoryName::OnFilesDropped), NULL, this);
    if (dropWindow2)
        dropWindow2->Connect(FFS_DROP_FILE_EVENT, FFSFileDropEventHandler(DirectoryName::OnFilesDropped), NULL, this);

    //keep dirPicker and dirName synchronous
    dirName_  .Connect(wxEVT_COMMAND_TEXT_UPDATED,      wxCommandEventHandler(      DirectoryName::OnWriteDirManually), NULL, this);
    dirPicker_.Connect(wxEVT_COMMAND_DIRPICKER_CHANGED, wxFileDirPickerEventHandler(DirectoryName::OnDirSelected     ), NULL, this);
}


template <class NameControl>
DirectoryName<NameControl>::~DirectoryName()
{
    dirName_  .Disconnect(wxEVT_COMMAND_TEXT_UPDATED,      wxCommandEventHandler(      DirectoryName::OnWriteDirManually), NULL, this);
    dirPicker_.Disconnect(wxEVT_COMMAND_DIRPICKER_CHANGED, wxFileDirPickerEventHandler(DirectoryName::OnDirSelected     ), NULL, this);
}


template <class NameControl>
void DirectoryName<NameControl>::OnFilesDropped(FFSFileDropEvent& event)
{
    if (event.getFiles().empty())
        return;

    if (acceptDrop(event.getFiles()))
    {
        const wxString fileName = event.getFiles()[0];
        if (dirExists(toZ(fileName)))
            setDirectoryName(fileName, &dirName_, &dirPicker_, dirName_, staticBox_);
        else
        {
            wxString parentName = beforeLast(fileName, utf8CvrtTo<wxString>(FILE_NAME_SEPARATOR)); //returns empty string if ch not found
#ifdef FFS_WIN
            if (endsWith(parentName, L":")) //volume name
                parentName += FILE_NAME_SEPARATOR;
#endif
            if (dirExists(toZ(parentName)))
                setDirectoryName(parentName, &dirName_, &dirPicker_, dirName_, staticBox_);
            else //set original name unconditionally: usecase: inactive mapped network shares
                setDirectoryName(fileName, &dirName_, &dirPicker_, dirName_, staticBox_);
        }
    }
}


template <class NameControl>
void DirectoryName<NameControl>::OnWriteDirManually(wxCommandEvent& event)
{
    setDirectoryName(event.GetString(), static_cast<NameControl*>(NULL), &dirPicker_, dirName_, staticBox_, 100); //potentially slow network access: wait 100 ms at most
    event.Skip();
}


template <class NameControl>
void DirectoryName<NameControl>::OnDirSelected(wxFileDirPickerEvent& event)
{
    const wxString newPath = event.GetPath();
    setDirectoryName(newPath, &dirName_, NULL, dirName_, staticBox_);
    event.Skip();
}


template <class NameControl>
wxString DirectoryName<NameControl>::getName() const
{
    return dirName_.GetValue();
}


template <class NameControl>
void DirectoryName<NameControl>::setName(const wxString& dirname)
{
    setDirectoryName(dirname, &dirName_, &dirPicker_, dirName_, staticBox_);
}


//explicit template instantiations
template class DirectoryName<wxTextCtrl>;
template class DirectoryName<FolderHistoryBox>;
bgstack15