android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / dbui / customizeaddresslistdialog.cxx
blobe80e75678c79490ed8866aca355c1dcd0dae152d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "customizeaddresslistdialog.hxx"
21 #include "createaddresslistdialog.hxx"
23 SwCustomizeAddressListDialog::SwCustomizeAddressListDialog(
24 weld::Window* pParent, const SwCSVData& rOldData)
25 : SfxDialogController(pParent, "modules/swriter/ui/customizeaddrlistdialog.ui",
26 "CustomizeAddrListDialog")
27 , m_xNewData(new SwCSVData(rOldData))
28 , m_xFieldsLB(m_xBuilder->weld_tree_view("treeview"))
29 , m_xAddPB(m_xBuilder->weld_button("add"))
30 , m_xDeletePB(m_xBuilder->weld_button("delete"))
31 , m_xRenamePB(m_xBuilder->weld_button("rename"))
32 , m_xUpPB(m_xBuilder->weld_button("up"))
33 , m_xDownPB(m_xBuilder->weld_button("down"))
35 m_xFieldsLB->set_size_request(-1, m_xFieldsLB->get_height_rows(14));
37 m_xFieldsLB->connect_changed(LINK(this, SwCustomizeAddressListDialog, ListBoxSelectHdl_Impl));
38 Link<weld::Button&,void> aAddRenameLk = LINK(this, SwCustomizeAddressListDialog, AddRenameHdl_Impl );
39 m_xAddPB->connect_clicked(aAddRenameLk);
40 m_xRenamePB->connect_clicked(aAddRenameLk);
41 m_xDeletePB->connect_clicked(LINK(this, SwCustomizeAddressListDialog, DeleteHdl_Impl ));
42 Link<weld::Button&,void> aUpDownLk = LINK(this, SwCustomizeAddressListDialog, UpDownHdl_Impl);
43 m_xUpPB->connect_clicked(aUpDownLk);
44 m_xDownPB->connect_clicked(aUpDownLk);
46 for (const auto& rHeader : m_xNewData->aDBColumnHeaders)
47 m_xFieldsLB->append_text(rHeader);
49 m_xFieldsLB->select(0);
50 UpdateButtons();
53 SwCustomizeAddressListDialog::~SwCustomizeAddressListDialog()
57 IMPL_LINK_NOARG(SwCustomizeAddressListDialog, ListBoxSelectHdl_Impl, weld::TreeView&, void)
59 UpdateButtons();
62 IMPL_LINK(SwCustomizeAddressListDialog, AddRenameHdl_Impl, weld::Button&, rButton, void)
64 bool bRename = &rButton == m_xRenamePB.get();
65 auto nPos = m_xFieldsLB->get_selected_index();
66 if (nPos == -1)
67 nPos = 0;
69 std::unique_ptr<SwAddRenameEntryDialog> xDlg;
70 if (bRename)
71 xDlg.reset(new SwRenameEntryDialog(m_xDialog.get(), m_xNewData->aDBColumnHeaders));
72 else
73 xDlg.reset(new SwAddEntryDialog(m_xDialog.get(), m_xNewData->aDBColumnHeaders));
74 if (bRename)
76 OUString aTemp = m_xFieldsLB->get_text(nPos);
77 xDlg->SetFieldName(aTemp);
79 if (xDlg->run() == RET_OK)
81 OUString sNew = xDlg->GetFieldName();
82 if(bRename)
84 m_xNewData->aDBColumnHeaders[nPos] = sNew;
85 m_xFieldsLB->remove(nPos);
87 else
89 if (m_xFieldsLB->get_selected_index() != -1)
90 ++nPos; // append the new entry behind the selected
91 //add the new column
92 m_xNewData->aDBColumnHeaders.insert(m_xNewData->aDBColumnHeaders.begin() + nPos, sNew);
93 //add a new entry into all data arrays
94 for (auto& rData : m_xNewData->aDBData)
95 rData.insert(rData.begin() + nPos, OUString());
99 m_xFieldsLB->insert_text(nPos, sNew);
100 m_xFieldsLB->select(nPos);
102 UpdateButtons();
105 IMPL_LINK_NOARG(SwCustomizeAddressListDialog, DeleteHdl_Impl, weld::Button&, void)
107 auto nPos = m_xFieldsLB->get_selected_index();
108 m_xFieldsLB->remove(nPos);
109 m_xFieldsLB->select(nPos > m_xFieldsLB->n_children() - 1 ? nPos - 1 : nPos);
111 //remove the column
112 m_xNewData->aDBColumnHeaders.erase(m_xNewData->aDBColumnHeaders.begin() + nPos);
113 //remove the data
114 for (auto& rData : m_xNewData->aDBData)
115 rData.erase(rData.begin() + nPos);
117 UpdateButtons();
120 IMPL_LINK(SwCustomizeAddressListDialog, UpDownHdl_Impl, weld::Button&, rButton, void)
122 auto nPos = m_xFieldsLB->get_selected_index();
123 auto nOldPos = nPos;
124 OUString aTemp = m_xFieldsLB->get_text(nPos);
125 m_xFieldsLB->remove(nPos);
126 if (&rButton == m_xUpPB.get())
127 --nPos;
128 else
129 ++nPos;
130 m_xFieldsLB->insert_text(nPos, aTemp);
131 m_xFieldsLB->select(nPos);
132 //align m_xNewData
133 OUString sHeader = m_xNewData->aDBColumnHeaders[nOldPos];
134 m_xNewData->aDBColumnHeaders.erase(m_xNewData->aDBColumnHeaders.begin() + nOldPos);
135 m_xNewData->aDBColumnHeaders.insert(m_xNewData->aDBColumnHeaders.begin() + nPos, sHeader);
136 for (auto& rData : m_xNewData->aDBData)
138 OUString sData = rData[nOldPos];
139 rData.erase(rData.begin() + nOldPos);
140 rData.insert(rData.begin() + nPos, sData);
143 UpdateButtons();
146 void SwCustomizeAddressListDialog::UpdateButtons()
148 auto nPos = m_xFieldsLB->get_selected_index();
149 auto nEntries = m_xFieldsLB->n_children();
150 m_xUpPB->set_sensitive(nPos > 0 && nEntries > 0);
151 m_xDownPB->set_sensitive(nPos < nEntries -1);
152 m_xDeletePB->set_sensitive(nEntries > 0);
153 m_xRenamePB->set_sensitive(nEntries > 0);
156 SwAddRenameEntryDialog::SwAddRenameEntryDialog(
157 weld::Window* pParent, const OUString& rUIXMLDescription, const OUString& rID,
158 const std::vector< OUString >& rCSVHeader)
159 : SfxDialogController(pParent, rUIXMLDescription, rID)
160 , m_rCSVHeader(rCSVHeader)
161 , m_xFieldNameED(m_xBuilder->weld_entry("entry"))
162 , m_xOK(m_xBuilder->weld_button("ok"))
164 m_xFieldNameED->connect_changed(LINK(this, SwAddRenameEntryDialog, ModifyHdl_Impl));
165 ModifyHdl_Impl(*m_xFieldNameED);
168 IMPL_LINK(SwAddRenameEntryDialog, ModifyHdl_Impl, weld::Entry&, rEdit, void)
170 OUString sEntry = rEdit.get_text();
171 bool bFound = sEntry.isEmpty();
173 if(!bFound)
175 bFound = std::any_of(m_rCSVHeader.begin(), m_rCSVHeader.end(),
176 [&sEntry](const OUString& rHeader) { return rHeader == sEntry; });
178 m_xOK->set_sensitive(!bFound);
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */