| 1 | # Copyright (C) 2016--2026 Karl Wette | ||
| 2 | # | ||
| 3 | # This file is part of App::PDFLibrarian. | ||
| 4 | # | ||
| 5 | # App::PDFLibrarian is free software: you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by | ||
| 7 | # the Free Software Foundation, either version 3 of the License, or (at | ||
| 8 | # your option) any later version. | ||
| 9 | # | ||
| 10 | # App::PDFLibrarian is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | # General Public License for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License | ||
| 16 | # along with App::PDFLibrarian. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | use v5.18; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use utf8; | ||
| 22 | use open qw(:std :encoding(UTF-8)); | ||
| 23 | |||
| 24 | package App::PDFLibrarian::QueryDialog::impl; | ||
| 25 | $App::PDFLibrarian::QueryDialog::impl::VERSION = '6.3.4'; | ||
| 26 | use Wx qw(:dialog :statictext :combobox :textctrl :sizer :panel :window :id); | ||
| 27 | use Wx::ArtProvider; | ||
| 28 | use Wx::Event qw(EVT_BUTTON EVT_TEXT EVT_TEXT_ENTER); | ||
| 29 | |||
| 30 | use base qw(Wx::Dialog); | ||
| 31 | |||
| 32 | use App::PDFLibrarian qw(%query_databases); | ||
| 33 | |||
| 34 | my $query_db_name_combo; | ||
| 35 | my $query_value_combo; | ||
| 36 | my $buttonok; | ||
| 37 | |||
| 38 | 1; | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my ($class, $pdffile, $query_db_name, $query_value, $query_values, $error_message) = @_; | ||
| 42 | |||
| 43 | # create dialog | ||
| 44 | my $self = $class->SUPER::new(undef, -1, "Import - PDFLibrarian", &Wx::wxDefaultPosition, &Wx::wxDefaultSize, wxDIALOG_NO_PARENT | wxDEFAULT_DIALOG_STYLE); | ||
| 45 | |||
| 46 | # set dialog icon | ||
| 47 | my $icon = Wx::ArtProvider::GetIcon(Wx::ArtProvider::wxART_QUESTION, Wx::ArtProvider::wxART_CMN_DIALOG); | ||
| 48 | $self->SetIcon($icon); | ||
| 49 | |||
| 50 | # create panel and sizer | ||
| 51 | my $topsizer = Wx::BoxSizer->new(wxVERTICAL); | ||
| 52 | my $panel = Wx::Panel->new($self, -1, [-1, -1], [-1, -1], wxTAB_TRAVERSAL | wxBORDER_NONE); | ||
| 53 | $panel->SetSizer($topsizer); | ||
| 54 | |||
| 55 | # add static text box for message | ||
| 56 | my $message; | ||
| 57 | if ($error_message ne '') { | ||
| 58 | $message = <<"EOM"; | ||
| 59 | App::PDFLibrarian has queries online database '$query_db_name' with the query value '$query_value'. Unfortunately the query returned the following errors: | ||
| 60 | --- | ||
| 61 | $error_message | ||
| 62 | --- | ||
| 63 | Please correct the query value and/or select a different online database, and try again. | ||
| 64 | |||
| 65 | EOM | ||
| 66 | } else { | ||
| 67 | $message = <<"EOM"; | ||
| 68 | App::PDFLibrarian would like to query an online database for a BibTeX record for the paper | ||
| 69 | |||
| 70 | $pdffile | ||
| 71 | |||
| 72 | in order to import the file into the PDF library. | ||
| 73 | |||
| 74 | Please select the online database below, and supply a query value which uniquely identified the paper. By default App::PDFLibrarian tries to extract a Digital Object Identifier from the PDF paper for use in the query, but this may well be incorrect and therefore should be double-checked. | ||
| 75 | |||
| 76 | EOM | ||
| 77 | } | ||
| 78 | $message .= <<"EOM"; | ||
| 79 | Please press the 'Run Query' button (or the Enter key) when ready to run the query; press the 'Manual Entry' button to manually enter the BibTeX record; press the 'Skip Import' button (or the Esc key) to skip the import of the PDF paper; or the 'Quit' button to cancel all imports and exit. | ||
| 80 | |||
| 81 | EOM | ||
| 82 | $topsizer->Add(Wx::StaticText->new($panel, -1, $message, [-1, -1], [500, 300]), 1, wxEXPAND | wxALL, 10); | ||
| 83 | |||
| 84 | # add read-only combo box for query database | ||
| 85 | my @query_db_names = sort { $a cmp $b } keys(%query_databases); | ||
| 86 | $query_db_name_combo = Wx::ComboBox->new($panel, -1, $query_db_name, [-1, -1], [500, 30], \@query_db_names, wxCB_READONLY | wxTE_PROCESS_ENTER); | ||
| 87 | $topsizer->Add($query_db_name_combo, 0, wxEXPAND | wxALL, 10); | ||
| 88 | |||
| 89 | # add editable combo box for DOI | ||
| 90 | $query_value_combo = Wx::ComboBox->new($panel, -1, $query_value, [-1, -1], [500, 30], \@{$query_values}, wxTE_PROCESS_ENTER); | ||
| 91 | $topsizer->Add($query_value_combo, 0, wxEXPAND | wxALL, 10); | ||
| 92 | |||
| 93 | # create buttons and sizer | ||
| 94 | my $buttonsizer = Wx::BoxSizer->new(wxHORIZONTAL); | ||
| 95 | $buttonok = Wx::Button->new($panel, wxID_OK, 'Run Query'); | ||
| 96 | $buttonsizer->Add($buttonok, 0, wxALL, 10); | ||
| 97 | my $buttonmanual = Wx::Button->new($panel, wxID_EDIT, 'Manual Entry'); | ||
| 98 | $buttonsizer->Add($buttonmanual, 0, wxALL, 10); | ||
| 99 | my $buttonskip = Wx::Button->new($panel, wxID_CANCEL, 'Skip Import'); | ||
| 100 | $buttonsizer->Add($buttonskip, 0, wxALL, 10); | ||
| 101 | my $buttonexit = Wx::Button->new($panel, wxID_EXIT, 'Quit'); | ||
| 102 | $buttonsizer->Add($buttonexit, 0, wxALL, 10); | ||
| 103 | |||
| 104 | # add buttons to sizer | ||
| 105 | $topsizer->Add($buttonsizer, 0, wxALIGN_CENTER); | ||
| 106 | |||
| 107 | # perform final layout | ||
| 108 | my $mainsizer = Wx::BoxSizer->new(wxVERTICAL); | ||
| 109 | $mainsizer->Add($panel, 1, wxEXPAND | wxALL, 0); | ||
| 110 | $self->SetSizerAndFit($mainsizer); | ||
| 111 | |||
| 112 | # register events | ||
| 113 | EVT_BUTTON($self, $buttonmanual, \&on_manual); | ||
| 114 | EVT_BUTTON($self, $buttonexit, \&on_exit); | ||
| 115 | EVT_TEXT($self, $query_db_name_combo, \&on_text); | ||
| 116 | EVT_TEXT($self, $query_value_combo, \&on_text); | ||
| 117 | EVT_TEXT_ENTER($self, $query_db_name_combo, \&on_enter); | ||
| 118 | EVT_TEXT_ENTER($self, $query_value_combo, \&on_enter); | ||
| 119 | on_text(); | ||
| 120 | |||
| 121 | return $self; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub get_data { | ||
| 125 | |||
| 126 | # get query database name | ||
| 127 | my $query_db_name_combo_value = $query_db_name_combo->GetValue(); | ||
| 128 | $query_db_name_combo_value =~ s/^\s+//; | ||
| 129 | $query_db_name_combo_value =~ s/\s+$//; | ||
| 130 | |||
| 131 | # get query value | ||
| 132 | my $query_value_combo_value = $query_value_combo->GetValue(); | ||
| 133 | $query_value_combo_value =~ s/^\s+//; | ||
| 134 | $query_value_combo_value =~ s/\s+$//; | ||
| 135 | |||
| 136 | return ($query_db_name_combo_value, $query_value_combo_value); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub on_text { | ||
| 140 | my ($self, $event) = @_; | ||
| 141 | |||
| 142 | # query database and query value | ||
| 143 | my ($query_db_name, $query_value) = get_data(); | ||
| 144 | |||
| 145 | # enable/disable "Run Query" button | ||
| 146 | $buttonok->Enable(length($query_db_name) > 0 && length($query_value) > 0); | ||
| 147 | |||
| 148 | } | ||
| 149 | |||
| 150 | sub on_enter { | ||
| 151 | my ($self, $event) = @_; | ||
| 152 | |||
| 153 | $self->EndModal(wxID_OK); | ||
| 154 | |||
| 155 | } | ||
| 156 | |||
| 157 | sub on_manual { | ||
| 158 | my ($self, $event) = @_; | ||
| 159 | |||
| 160 | $self->EndModal(wxID_EDIT); | ||
| 161 | |||
| 162 | } | ||
| 163 | |||
| 164 | sub on_exit { | ||
| 165 | my ($self, $event) = @_; | ||
| 166 | |||
| 167 | $self->EndModal(wxID_EXIT); | ||
| 168 | |||
| 169 | } |