1 /* This file is part of the KDE project
2  * Copyright (C) 2011 Smit Patel <smitpatel24@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #include "CitationInsertionDialog.h"
20 
21 #include <QDebug>
22 #include <KoInlineCite.h>
23 #include <KoInlineTextObjectManager.h>
24 #include <KoTextDocument.h>
25 
26 #include <QMessageBox>
27 
CitationInsertionDialog(KoTextEditor * editor,QWidget * parent)28 CitationInsertionDialog::CitationInsertionDialog(KoTextEditor *editor ,QWidget *parent) :
29     QDialog(parent),
30     m_blockSignals(false),
31     m_editor(editor)
32 {
33     dialog.setupUi(this);
34     connect(dialog.buttonBox,SIGNAL(accepted()),this,SLOT(insert()));
35     connect(dialog.existingCites,SIGNAL(currentIndexChanged(QString)),this,SLOT(selectionChangedFromExistingCites()));
36 
37     QStringList existingCites(i18n("Select"));
38     foreach (KoInlineCite *cite, KoTextDocument(m_editor->document()).inlineTextObjectManager()->citations().values()) {
39         existingCites << cite->identifier();
40         m_cites[cite->identifier()] = cite;
41     }
42     existingCites.removeDuplicates();
43     dialog.existingCites->addItems(existingCites);
44 
45     show();
46 }
47 
insert()48 void CitationInsertionDialog::insert()
49 {
50     if (m_cites.contains(dialog.shortName->text())) {
51         if (*m_cites.value(dialog.shortName->text()) != *toCite()) {      //prompts if values are changed
52             int ret = QMessageBox::warning(this,i18n("Warning"),i18n("The document already contains the bibliography entry with different data.\n"
53                                  "Do you want to adjust existing entries?"),QMessageBox::Yes | QMessageBox::No);
54             if ( ret == QMessageBox::Yes) {
55                 foreach(KoInlineCite *existingCite, m_cites.values(dialog.shortName->text())) {
56                     *existingCite = *toCite();                       //update all cites with new values
57                     existingCite->setType(KoInlineCite::ClonedCitation);    //change type to ClonedCitation
58                 }
59                 emit accept();
60             } else return;
61         }
62     }
63     KoInlineCite *cite = m_editor->insertCitation();
64     if (dialog.shortName->text().isEmpty()) {
65         const int number =
66             KoTextDocument(m_editor->document()).inlineTextObjectManager()->citations().count();
67         dialog.shortName->setText(i18n("Short name%1", number));
68 
69         dialog.shortName->setSelection(dialog.shortName->text().length(),0);
70     }
71     *cite = *toCite();
72     emit accept();
73 }
74 
selectionChangedFromExistingCites()75 void CitationInsertionDialog::selectionChangedFromExistingCites()
76 {
77     if (dialog.existingCites->currentIndex() != 0) {
78         KoInlineCite *cite = m_cites[dialog.existingCites->currentText()];
79         this->fillValuesFrom(cite);
80     } else if (dialog.existingCites->currentIndex() == 0) {
81         KoInlineCite *blankCite = new KoInlineCite(KoInlineCite::Citation);
82         blankCite->setBibliographyType("Article");      //default bibliography type
83         const int number =
84             KoTextDocument(m_editor->document()).inlineTextObjectManager()->citations().count() + 1;
85         blankCite->setIdentifier(i18n("Short name%1", number));
86         fillValuesFrom(blankCite);
87     }
88 }
89 
toCite()90 KoInlineCite *CitationInsertionDialog::toCite()
91 {
92     KoInlineCite *cite = new KoInlineCite(KoInlineCite::Citation);
93     cite->setAddress(dialog.address->text());
94     cite->setAnnotation(dialog.annotation->text());
95     cite->setAuthor(dialog.author->text());
96     cite->setBibliographyType(dialog.sourceType->currentText().remove(QLatin1Char(' ')).toLower());      //removing spaces and lowering case for exact tag attribute of bibliography-type
97     cite->setBookTitle(dialog.booktitle->text());
98     cite->setChapter(dialog.chapter->text());
99     cite->setCustom1(dialog.ud1->text());
100     cite->setCustom2(dialog.ud2->text());
101     cite->setCustom3(dialog.ud3->text());
102     cite->setCustom4(dialog.ud4->text());
103     cite->setCustom5(dialog.ud5->text());
104     cite->setEdition(dialog.edition->text());
105     cite->setEditor(dialog.editor->text());
106     cite->setIdentifier(dialog.shortName->text());
107     cite->setInstitution(dialog.institution->text());
108     cite->setISBN(dialog.isbn->text());
109     cite->setISSN(dialog.issn->text());
110     cite->setJournal(dialog.journal->text());
111     cite->setMonth(dialog.month->text());
112     cite->setNote(dialog.note->text());
113     cite->setNumber(dialog.number->text());
114     cite->setOrganisation(dialog.organisation->text());
115     cite->setPages(dialog.pages->text());
116     cite->setPublicationType(dialog.publication->text());
117     cite->setPublisher(dialog.publisher->text());
118     cite->setReportType(dialog.reporttype->text());
119     cite->setSchool(dialog.school->text());
120     cite->setSeries(dialog.series->text());
121     cite->setTitle(dialog.title->text());
122     cite->setURL(dialog.url->text());
123     cite->setVolume(dialog.volume->text());
124     cite->setYear(dialog.year->text());
125     return cite;
126 }
127 
fillValuesFrom(KoInlineCite * cite)128 void CitationInsertionDialog::fillValuesFrom(KoInlineCite *cite)
129 {
130     dialog.address->setText(cite->address());
131     dialog.annotation->setText(cite->annotation());
132     dialog.author->setText(cite->author());
133     dialog.sourceType->setCurrentIndex(dialog.sourceType->findText(cite->bibliographyType(),Qt::MatchFixedString));
134     dialog.booktitle->setText(cite->bookTitle());
135     dialog.chapter->setText(cite->chapter());
136     dialog.ud1->setText(cite->custom1());
137     dialog.ud2->setText(cite->custom2());
138     dialog.ud3->setText(cite->custom3());
139     dialog.ud4->setText(cite->custom4());
140     dialog.ud5->setText(cite->custom5());
141     dialog.edition->setText(cite->edition());
142     dialog.editor->setText(cite->editor());
143     dialog.institution->setText(cite->institution());
144     dialog.shortName->setText(cite->identifier());
145     dialog.isbn->setText(cite->isbn());
146     dialog.issn->setText(cite->issn());
147     dialog.journal->setText(cite->journal());
148     dialog.month->setText(cite->month());
149     dialog.note->setText(cite->note());
150     dialog.number->setText(cite->number());
151     dialog.organisation->setText(cite->organisations());
152     dialog.pages->setText(cite->pages());
153     dialog.publication->setText(cite->publicationType());
154     dialog.publisher->setText(cite->publisher());
155     dialog.school->setText(cite->school());
156     dialog.series->setText(cite->series());
157     dialog.title->setText(cite->title());
158     dialog.reporttype->setText(cite->reportType());
159     dialog.volume->setText(cite->volume());
160     dialog.year->setText(cite->year());
161     dialog.url->setText(cite->url());
162 }
163