1 #include "filepropsdlg.h"
2 #include "ui_filepropsdlg.h"
3 #include "cr3widget.h"
4 #include "../crengine/include/lvdocview.h"
5 #include <cr3version.h>
6 
7 #if QT_VERSION >= 0x050000
8 #define setResizeModeMethod setSectionResizeMode
9 #else
10 #define setResizeModeMethod setResizeMode
11 #endif
12 
FilePropsDialog(QWidget * parent,CR3View * docView)13 FilePropsDialog::FilePropsDialog(QWidget *parent, CR3View * docView ) :
14     QDialog(parent),
15     m_ui(new Ui::FilePropsDialog)
16     ,_cr3v(docView)
17     ,_docview(docView->getDocView())
18 {
19     m_ui->setupUi(this);
20     setWindowTitle( tr("Document properties") );
21 
22     m_ui->tableWidget->setColumnCount(2);
23     m_ui->tableWidget->setHorizontalHeaderLabels ( QStringList() << tr("Property") << tr("Value") );
24     m_ui->tableWidget->verticalHeader()->hide();
25     m_ui->tableWidget->horizontalHeader()->setResizeModeMethod( 0, QHeaderView::ResizeToContents );
26     m_ui->tableWidget->horizontalHeader()->setResizeModeMethod( 1, QHeaderView::Stretch ); //Stretch
27     m_ui->tableWidget->horizontalHeader()->setStretchLastSection( true );
28     m_ui->tableWidget->horizontalHeader()->setDefaultAlignment( Qt::AlignLeft );
29     m_ui->tableWidget->setEditTriggers( QAbstractItemView::NoEditTriggers );
30 
31     m_ui->tableWidget->setWordWrap(true);
32     m_ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
33     m_ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
34     m_ui->tableWidget->setSortingEnabled(false);
35 
36     //m_ui->tableWidget->setRowCount(1);
37 
38     fillItems();
39     _cr3v->restoreWindowPos( this, "fileprops." );
40 }
41 
closeEvent(QCloseEvent * event)42 void FilePropsDialog::closeEvent ( QCloseEvent * event )
43 {
44     _cr3v->saveWindowPos( this, "fileprops." );
45 }
46 
~FilePropsDialog()47 FilePropsDialog::~FilePropsDialog()
48 {
49     delete m_ui;
50 }
51 
showDlg(QWidget * parent,CR3View * docView)52 bool FilePropsDialog::showDlg( QWidget * parent, CR3View * docView )
53 {
54     FilePropsDialog * dlg = new FilePropsDialog( parent, docView );
55     dlg->setModal( true );
56     dlg->show();
57     dlg->raise();
58     dlg->activateWindow();
59     //dlg->
60     return true;
61 }
62 
getDocText(const char * path,const char * delim)63 QString FilePropsDialog::getDocText( const char * path, const char * delim )
64 {
65     ldomDocument * doc = _docview->getDocument();
66     lString32 res;
67     for ( int i=0; i<100; i++ ) {
68         lString8 p = lString8(path) + "[" + fmt::decimal(i+1) + "]";
69         //CRLog::trace("checking doc path %s", p.c_str() );
70         lString32 p32 = Utf8ToUnicode(p);
71         ldomXPointer ptr = doc->createXPointer( p32 );
72         if ( ptr.isNull() )
73             break;
74         lString32 s = ptr.getText( U' ' );
75         if ( s.empty() )
76             continue;
77         if ( !res.empty() && delim!=NULL )
78             res << Utf8ToUnicode( lString8( delim ) );
79         res << s;
80     }
81     return cr2qt(res);
82 }
83 
getDocAuthors(const char * path,const char * delim)84 QString FilePropsDialog::getDocAuthors( const char * path, const char * delim )
85 {
86     lString32 res;
87     for ( int i=0; i<100; i++ ) {
88         lString8 p = lString8(path) + "[" + fmt::decimal(i+1) + "]";
89         //CRLog::trace("checking doc path %s", p.c_str() ));
90         lString32 firstName = qt2cr(getDocText( (p + "/first-name").c_str(), " " ));
91         lString32 lastName = qt2cr(getDocText( (p + "/last-name").c_str(), " " ));
92         lString32 middleName = qt2cr(getDocText( (p + "/middle-name").c_str(), " " ));
93         lString32 nickName = qt2cr(getDocText( (p + "/nickname").c_str(), " " ));
94         lString32 homePage = qt2cr(getDocText( (p + "/homepage").c_str(), " " ));
95         lString32 email = qt2cr(getDocText( (p + "/email").c_str(), " " ));
96         lString32 s = firstName;
97         if ( !middleName.empty() )
98             s << " " << middleName;
99         if ( !lastName.empty() ) {
100             if ( !s.empty() )
101                 s << " ";
102             s << lastName;
103         }
104         if ( !nickName.empty() ) {
105             if ( !s.empty() )
106                 s << " ";
107             s << nickName;
108         }
109         if ( !homePage.empty() ) {
110             if ( !s.empty() )
111                 s << " ";
112             s << homePage;
113         }
114         if ( !email.empty() ) {
115             if ( !s.empty() )
116                 s << " ";
117             s << email;
118         }
119         if ( s.empty() )
120             continue;
121         if ( !res.empty() && delim!=NULL )
122             res << Utf8ToUnicode( lString8( delim ) );
123         res << s;
124     }
125     return cr2qt(res);
126 }
127 
addPropLine(QString name,QString v)128 void FilePropsDialog::addPropLine( QString name, QString v )
129 {
130     v = v.trimmed();
131     if ( v.length()==0 )
132         return;
133     prop.append(name);
134     value.append(v);
135 }
136 
addInfoSection(QString name)137 void FilePropsDialog::addInfoSection( QString name )
138 {
139     if ( prop.length()==0 )
140         return;
141     int y = m_ui->tableWidget->rowCount();
142     m_ui->tableWidget->setRowCount(y+1);
143     m_ui->tableWidget->setItem( y, 0, new QTableWidgetItem(name));
144     m_ui->tableWidget->setSpan( y, 0, 1, 2 );
145     //m_ui->tableWidget->setItem( y, 1, new QTableWidgetItem(value));
146     for ( int i=0; i<prop.length(); i++ ) {
147         int y = m_ui->tableWidget->rowCount();
148         m_ui->tableWidget->setRowCount(y+1);
149         m_ui->tableWidget->setItem( y, 0, new QTableWidgetItem(prop[i]));
150         m_ui->tableWidget->setItem( y, 1, new QTableWidgetItem(value[i]));
151         m_ui->tableWidget->verticalHeader()->setResizeModeMethod( y, QHeaderView::ResizeToContents );
152     }
153     prop.clear();
154     value.clear();
155 }
156 
fillItems()157 void FilePropsDialog::fillItems()
158 {
159     _docview->savePosition();
160     CRFileHistRecord * hist = _docview->getCurrentFileHistRecord();
161 
162     lString32 title("Cool Reader ");
163 #ifndef PACKAGE_VERSION
164 #define PACKAGE_VERSION CR_ENGINE_VERSION
165 #endif
166     title << Utf8ToUnicode(lString8(PACKAGE_VERSION));
167 
168     lString8 txt;
169     //=========================================================
170     txt << "<table><col width=\"25%\"/><col width=\"75%\"/>\n";
171     CRPropRef props = _docview->getDocProps();
172 
173     addPropLine( tr("Current page"), cr2qt(lString32::itoa(_docview->getCurPage())) );
174     addPropLine( tr("Total pages"), cr2qt(lString32::itoa(_docview->getPageCount())) );
175     addPropLine( tr("Battery state"), cr2qt(lString32::itoa(_docview->getBatteryState()) + "%") );
176     addPropLine( tr("Current Time"), cr2qt(_docview->getTimeString()) );
177     // TODO:
178     if ( hist ) {
179         CRBookmark * lastpos = hist->getLastPos();
180         if ( lastpos ) {
181             addPropLine( tr("Current chapter"), cr2qt(lastpos->getTitleText()));
182         }
183     }
184     addInfoSection( tr("Status") );
185 
186     addPropLine( tr("Archive name"), cr2qt(props->getStringDef(DOC_PROP_ARC_NAME)) );
187     addPropLine( tr("Archive path"), cr2qt(props->getStringDef(DOC_PROP_ARC_PATH)) );
188     addPropLine( tr("Archive size"), cr2qt(props->getStringDef(DOC_PROP_ARC_SIZE)) );
189     addPropLine( tr("File name"), cr2qt(props->getStringDef(DOC_PROP_FILE_NAME)) );
190     addPropLine( tr("File path"), cr2qt(props->getStringDef(DOC_PROP_FILE_PATH)) );
191     addPropLine( tr("File size"), cr2qt(props->getStringDef(DOC_PROP_FILE_SIZE)) );
192     addPropLine( tr("File format"), cr2qt(props->getStringDef(DOC_PROP_FILE_FORMAT)) );
193     addInfoSection( tr("File info") );
194 
195     addPropLine( tr("Title"), cr2qt(props->getStringDef(DOC_PROP_TITLE)) );
196     addPropLine( tr("Author(s)"), cr2qt(props->getStringDef(DOC_PROP_AUTHORS)) );
197     addPropLine( tr("Series name"), cr2qt(props->getStringDef(DOC_PROP_SERIES_NAME)) );
198     addPropLine( tr("Series number"), cr2qt(props->getStringDef(DOC_PROP_SERIES_NUMBER)) );
199     addPropLine( tr("Date"), getDocText( "/FictionBook/description/title-info/date", ", " ) );
200     addPropLine( tr("Genres"), getDocText( "/FictionBook/description/title-info/genre", ", " ) );
201     addPropLine( tr("Translator"), getDocText( "/FictionBook/description/title-info/translator", ", " ) );
202     addInfoSection( tr("Book info") );
203 
204     addPropLine( tr("Document author"), getDocAuthors( "/FictionBook/description/document-info/author", " " ) );
205     addPropLine( tr("Document date"), getDocText( "/FictionBook/description/document-info/date", " " ) );
206     addPropLine( tr("Document source URL"), getDocText( "/FictionBook/description/document-info/src-url", " " ) );
207     addPropLine( tr("OCR by"), getDocText( "/FictionBook/description/document-info/src-ocr", " " ) );
208     addPropLine( tr("Document version"), getDocText( "/FictionBook/description/document-info/version", " " ) );
209     addInfoSection( tr("Document info") );
210 
211     addPropLine( tr("Publication name"), getDocText( "/FictionBook/description/publish-info/book-name", " " ) );
212     addPropLine( tr("Publisher"), getDocText( "/FictionBook/description/publish-info/publisher", " " ) );
213     addPropLine( tr("Publisher city"), getDocText( "/FictionBook/description/publish-info/city", " " ) );
214     addPropLine( tr("Publication year"), getDocText( "/FictionBook/description/publish-info/year", " " ) );
215     addPropLine( tr("ISBN"), getDocText( "/FictionBook/description/publish-info/isbn", " " ) );
216     addInfoSection( tr("Publication info") );
217 
218     addPropLine( tr("Custom info"), getDocText( "/FictionBook/description/custom-info", " " ) );
219 
220 }
221 
222 
changeEvent(QEvent * e)223 void FilePropsDialog::changeEvent(QEvent *e)
224 {
225     QDialog::changeEvent(e);
226     switch (e->type()) {
227     case QEvent::LanguageChange:
228         m_ui->retranslateUi(this);
229         break;
230     default:
231         break;
232     }
233 }
234