summaryrefslogtreecommitdiffstats
path: root/src/savequerydialog.h
blob: a91654ea247328f59a58dc9101d70f1b7eb3148c (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
/*
 * qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs
 * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#pragma once

#include <QDialog>

#include "config.h"

class QComboBox;
class QLabel;
class QLineEdit;
class QPushButton;

/// Names a query and keeps it in queries.json.
///
/// Pure UI: it is handed the config and returns a SavedQuery. It writes
/// nothing, so it can be tested without touching a file, and the caller owns
/// the decision of what to do with the result.
class SaveQueryDialog : public QDialog
{
    Q_OBJECT
public:
    /// `query` is the text to store, `accountKey` the scope to preselect,
    /// which is the account the user is already looking at.
    SaveQueryDialog(const Config &config, const QString &query,
                    const QString &accountKey, QWidget *parent = nullptr);

    /// Edits an entry that already exists, prefilled from it rather than from
    /// the query bar.
    SaveQueryDialog(const Config &config, const SavedQuery &existing,
                    QWidget *parent = nullptr);

    /// The query as edited. Only meaningful after exec() returned Accepted.
    SavedQuery savedQuery() const;

    /// Whether `name` already names a stored query. Case-insensitive, matching
    /// how startup_query resolves, so "inbox" and "Inbox" cannot both exist and
    /// leave the user unable to tell which one a button ran.
    static bool namesAnExistingQuery(const Config &config, const QString &name);

private:
    void build(const SavedQuery &initial);
    void updateOkState();

    /// The name the dialog was opened on, empty when creating. The caller
    /// matches on this rather than on the returned name, so a rename replaces
    /// the entry instead of adding a second one beside it.
    QString m_originalName;

    /// Set for a generated entry, whose query is composed from the accounts
    /// and cannot be edited here.
    QString m_generated;
    bool m_flat = false;

    const Config &m_config;
    QLineEdit *m_name = nullptr;
    QLineEdit *m_query = nullptr;
    QComboBox *m_account = nullptr;
    QPushButton *m_ok = nullptr;
    QLabel *m_notice = nullptr;
};