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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
/***************************************************************************
* QuickpostTemplateDialog.cc
*
* Copyright (C) 2007, 2008, Matthew J Smith
*
* This file is part of QTM.
* QTM 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*****************************************************************************/
#include <QWidget>
#include <QDialog>
#include <QListWidget>
#include <QListWidgetItem>
#include <QComboBox>
#include <QString>
#include <QStringList>
#include <QList>
#include <QWhatsThis>
#include <QMessageBox>
#include <QModelIndex>
#include <QLineEdit>
#include <QTextEdit>
#include "QuickpostTemplateDialog.h"
#include "ui_QuickpostTemplateForm.h"
QuickpostTemplateDialog::QuickpostTemplateDialog( QStringList &titles,
QStringList &templates,
QList<int> &pubStates,
QList<bool> &ctStates,
QList<QStringList> &ahLists,
bool status,
QWidget *parent )
: QDialog( parent )
{
setupUi( this );
teTemplateText->setWhatsThis( tr( "<p>Enter the text of your template here, in HTML "
"format.</p>"
"<p>Four place-markers are supported:</p>"
"<p><strong>%title%</strong> - The page's title<br />"
"<strong>%url%</strong> - the full web location (URL) of "
"the page being blogged;<br />"
"<strong>%host%</strong> - the page's host name (after http://), and<br />"
"<strong>%domain%</strong> - the page's hostname without the \"www\".</p>" ) );
lTemplate->setWhatsThis( tr( "<p>Enter the text of your template here, in HTML "
"format.</p>"
"<p>Four place-markers are supported:</p>"
"<p><strong>%title%</strong> - The page's title<br />"
"<strong>%url%</strong> - the full web location (URL) of "
"the page being blogged;<br />"
"<strong>%host%</strong> - the page's host name (after http://), and<br />"
"<strong>%domain%</strong> - the page's hostname without the \"www\".</p>" ) );
cbDefaultPublishStatus->setWhatsThis( tr( "<p>Set this to Draft or Publish if you always want "
"posts based on this template to have that status.</p>"
"<p>Otherwise, the post will have the status you specify in "
"the Preferences window.</p>" ) );
lDefaultPublishStatus->setWhatsThis( tr( "<p>Set this to Draft or Publish if you always want "
"posts based on this template to have that status.</p>"
"<p>Otherwise, the post will have the status you specify in "
"the Preferences window.</p>" ) );
_templateTitles = titles;
_templateStrings = templates;
_defaultPublishStates = pubStates;
_copyTitleStates = ctStates;
_assocHostLists = ahLists;
_assocHostStrings = compileAssocHostStrings( _assocHostLists );
originalTitles = titles;
originalStrings = templates;
originalPublishStates = pubStates;
originalCopyTitleStates = ctStates;
originalAssocHostLists = ahLists;
hboxLayout->setStretchFactor( hboxLayout->itemAt( 0 )->layout(), 2 );
hboxLayout->setStretchFactor( hboxLayout->itemAt( 1 )->layout(), 3 );
Q_FOREACH( QString a, _templateTitles ) {
if( a.isEmpty() )
lwTemplates->addItem( tr( "(No name)" ) );
else
lwTemplates->addItem( a );
}
//qDebug( "added the templates" );
pbReset->setVisible( false );
leName->setEnabled( false );
teTemplateText->setEnabled( false );
teAssocHosts->setEnabled( false );
cbDefaultPublishStatus->setEnabled( false );
chCopyTitle->setEnabled( false );
doingNewTemplate = false;
// setClean();
currentRow = -1;
defaultCPStatus = status;
addTemplate = new QAction( tr( "&Add template" ), lwTemplates );
removeTemplate = new QAction( tr( "&Remove this template" ), lwTemplates );
lwTemplates->addAction( addTemplate );
lwTemplates->addAction( removeTemplate );
lwTemplates->setContextMenuPolicy( Qt::ActionsContextMenu );
insertTabIntoTemplate = new QAction( teTemplateText );
insertTabIntoTemplate->setShortcut( tr( "Ctrl+T" ) );
insertTabIntoTemplate->setShortcutContext( Qt::WidgetShortcut );
teTemplateText->addAction( insertTabIntoTemplate );
connect( addTemplate, SIGNAL( triggered( bool ) ),
this, SLOT( doNewTemplate() ) );
connect( removeTemplate, SIGNAL( triggered( bool ) ),
this, SLOT( removeThisTemplate() ) );
connect( lwTemplates, SIGNAL( currentRowChanged( int ) ),
this, SLOT( changeListIndex( int ) ) );
connect( insertTabIntoTemplate, SIGNAL( triggered( bool ) ),
this, SLOT( doInsertTabIntoTemplate() ) );
}
/*void QuickpostTemplateDialog::on_lwTemplates_clicked( const QModelIndex &ind )
{
qDebug( "lwTemplates clicked" );
int row = ind.row();
if( row != currentRow ) {
if( dirty ) {
qDebug( "status is dirty" );
int ret = QMessageBox::warning( 0, tr("QTM"),
tr("The template has been modified.\n"
"Do you want to save your changes?"),
tr( "&Save" ), tr( "&Discard" ),
tr( "&Cancel" ), 0, 2 );
switch( ret ) {
case 2:
return; // Click will have no effect
case 0:
acceptTemplate();
default:
lwTemplates->setCurrentRow( row );
changeListIndex( row );
}
}
else {
lwTemplates->setCurrentRow( row );
changeListIndex( row );
}
}
else
qDebug( "row is current row" );
}*/
void QuickpostTemplateDialog::changeListIndex( int index )
{
if( doingNewTemplate ) {
if( dirty )
lwTemplates->item( (lwTemplates->count())-1 )->setText( leName->text().isEmpty() ?
tr( "(No name)" ) :
leName->text() );
else {
_templateTitles.removeAt( currentRow );
_templateStrings.removeAt( currentRow );
_defaultPublishStates.removeAt( currentRow );
_assocHostStrings.removeAt( currentRow );
_copyTitleStates.removeAt( currentRow );
lwTemplates->takeItem( lwTemplates->count()-1 );
}
}
doingNewTemplate = false;
currentRow = index;
currentTemplateTitle = _templateTitles[currentRow];
currentTemplateString = _templateStrings[currentRow];
leName->setText( _templateTitles[currentRow] );
teTemplateText->setPlainText( _templateStrings[currentRow] );
cbDefaultPublishStatus->setCurrentIndex( _defaultPublishStates[currentRow] );
chCopyTitle->setCheckState( _copyTitleStates[currentRow] ? Qt::Checked : Qt::Unchecked );
teAssocHosts->setPlainText( _assocHostStrings[currentRow] );
leName->setEnabled( true );
teTemplateText->setEnabled( true );
cbDefaultPublishStatus->setEnabled( true );
chCopyTitle->setEnabled( true );
teAssocHosts->setEnabled( true );
// setClean();
}
void QuickpostTemplateDialog::doNewTemplate()
{
leName->setEnabled( true );
teTemplateText->setEnabled( true );
cbDefaultPublishStatus->setCurrentIndex( 2 );
if( cbDefaultPublishStatus->count() == 4 ) // if it's been disabled and blank
cbDefaultPublishStatus->removeItem( 3 ); // remove the blank item
cbDefaultPublishStatus->setEnabled( true );
chCopyTitle->setEnabled( true );
teAssocHosts->setEnabled( true );
// pbAccept->setEnabled( true );
_templateTitles.append( QString() );
_templateStrings.append( QString() );
_defaultPublishStates.append( int( 2 ) );
_assocHostStrings.append( QString() );
_copyTitleStates.append( bool( defaultCPStatus ) );
lwTemplates->disconnect( SIGNAL( currentRowChanged( int ) ),
this, SLOT( changeListIndex( int ) ) );
currentRow = -1;
leName->clear();
teTemplateText->clear();
teAssocHosts->clear();
chCopyTitle->setChecked( defaultCPStatus );
lwTemplates->addItem( tr( "New template" ) );
currentRow = lwTemplates->count()-1;
lwTemplates->setCurrentRow( lwTemplates->count()-1 );
connect( lwTemplates, SIGNAL( currentRowChanged( int ) ),
this, SLOT( changeListIndex( int ) ) );
doingNewTemplate = true;
leName->setFocus( Qt::OtherFocusReason );
setClean();
}
void QuickpostTemplateDialog::removeThisTemplate()
{
int c = lwTemplates->currentRow();
// lwTemplates->takeItem( c );
if( lwTemplates->count() == 1 ) {
lwTemplates->disconnect( SIGNAL( currentRowChanged( int ) ) );
lwTemplates->clear();
_templateTitles.clear();
_templateStrings.clear();
_defaultPublishStates.clear();
_copyTitleStates.clear();
_assocHostStrings.clear();
leName->clear();
leName->setEnabled( false );
teTemplateText->clear();
teTemplateText->setEnabled( false );
cbDefaultPublishStatus->addItem( "" );
cbDefaultPublishStatus->setCurrentIndex( 3 );
cbDefaultPublishStatus->setEnabled( false );
teAssocHosts->clear();
teAssocHosts->setEnabled( false );
chCopyTitle->setEnabled( false );
// pbAccept->setEnabled( false );
currentRow = -1;
} else {
lwTemplates->takeItem( c );
_templateTitles.removeAt( c );
_templateStrings.removeAt( c );
_defaultPublishStates.removeAt( c );
_copyTitleStates.removeAt( c );
_assocHostStrings.removeAt( c );
}
// setClean();
// qDebug( "exiting" );
/*if( originalTitles.size() < lowestNumber )
lowestNumber = titles.size(); */
}
void QuickpostTemplateDialog::doInsertTabIntoTemplate()
{
teTemplateText->insertPlainText( "\t" );
}
void QuickpostTemplateDialog::setDirty()
{
dirty = true;
disconnect( leName, 0, this, SLOT( setDirty() ) );
disconnect( teTemplateText, 0, this, SLOT( setDirty() ) );
disconnect( teAssocHosts, 0, this, SLOT( setDirty() ) );
disconnect( cbDefaultPublishStatus, 0, this, SLOT( setDirty() ) );
disconnect( chCopyTitle, 0, this, SLOT( setDirty() ) );
// disconnect( this, SLOT( setDirty() ) );
}
void QuickpostTemplateDialog::setClean()
{
// qDebug( "clean" );
dirty = false;
// disconnect( this, SLOT( setDirty() ) ); // to prevent duplicates
disconnect( leName, 0, this, SLOT( setDirty() ) );
disconnect( teTemplateText, 0, this, SLOT( setDirty() ) );
disconnect( teAssocHosts, 0, this, SLOT( setDirty() ) );
disconnect( cbDefaultPublishStatus, 0, this, SLOT( setDirty() ) );
disconnect( chCopyTitle, 0, this, SLOT( setDirty() ) );
connect( leName, SIGNAL( textChanged( const QString & ) ),
this, SLOT( setDirty() ) );
connect( teTemplateText, SIGNAL( textChanged() ),
this, SLOT( setDirty() ) );
connect( teAssocHosts, SIGNAL( textChanged() ),
this, SLOT( setDirty() ) );
connect( cbDefaultPublishStatus, SIGNAL( currentIndexChanged( int ) ),
this, SLOT( setDirty() ) );
connect( chCopyTitle, SIGNAL( clicked( bool ) ),
this, SLOT( setDirty() ) );
}
void QuickpostTemplateDialog::acceptTemplate()
{
if( doingNewTemplate ) {
_templateTitles.append( leName->text() );
_templateStrings.append( teTemplateText->toPlainText() );
_defaultPublishStates.append( cbDefaultPublishStatus->currentIndex() );
_copyTitleStates.append( chCopyTitle->isChecked() );
_assocHostStrings.append( teAssocHosts->toPlainText().trimmed() );
lwTemplates->item( lwTemplates->count()-1 )->setText( leName->text().isEmpty() ?
tr( "(No name)" ) :
leName->text() );
if( lwTemplates->count() == 1 )
connect( lwTemplates, SIGNAL( currentRowChanged( int ) ),
this, SLOT( changeListIndex( int ) ) );
/* QListWidgetItem *newItem = new QListWidgetItem( leName->text() );
lwTemplates->addItem( newItem );
lwTemplates->setCurrentItem( newItem );*/
} else {
if( lwTemplates->count() ) {
lwTemplates->item( currentRow )->setText( leName->text().isEmpty() ?
tr( "(No name)" ) : leName->text() );
/* _templateTitles[currentRow] = leName->text();
_templateStrings[currentRow] = teTemplateText->toPlainText();
_defaultPublishStates[currentRow] = cbDefaultPublishStatus->currentIndex();
_copyTitleStates[currentRow] = chCopyTitle->isChecked();
_assocHostLists[currentRow] = teAssocHosts->toPlainText().trimmed().split( QRegExp( "\\n+" ) ); */
}
}
doingNewTemplate = false;
setClean();
// qDebug( "Finished accept routine" );
}
void QuickpostTemplateDialog::on_pbWhatsThis_clicked()
{
QWhatsThis::enterWhatsThisMode();
}
/*void QuickpostTemplateDialog::on_pbReset_clicked()
{
if( QMessageBox::question( 0, tr( "QTM" ),
tr( "Are you sure you wish to lose all your changes?" ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No )
== QMessageBox::Yes ) {
lwTemplates->disconnect( SIGNAL( currentRowChanged( int ) ) );
lwTemplates->clear();
qDebug( "cleared templates list widget" );
leName->setText( QString() );
qDebug( "cleared name" );
teTemplateText->clear();
qDebug( "starting to rewrite lists" );
_templateTitles = originalTitles;
qDebug( "rewritten titles" );
_templateStrings = originalStrings;
_defaultPublishStates = originalPublishStates;
_copyTitleStates = originalCopyTitleStates;
_assocHostLists = originalAssocHostLists;
_assocHostStrings = compileAssocHostStrings( _assocHostLists );
qDebug( "rewritten all lists" );
lwTemplates->addItems( _templateTitles );
//qDebug( "added titles to list " );
leName->clear();
teTemplateText->clear();
teAssocHosts->clear();
chCopyTitle->setChecked( false );
chCopyTitle->setEnabled( false );
leName->setEnabled( false );
teTemplateText->setEnabled( false );
teAssocHosts->setEnabled( false );
doingNewTemplate = false;
connect( lwTemplates, SIGNAL( currentRowChanged( int ) ),
this, SLOT( changeListIndex( int ) ) );
setClean();
if( lwTemplates->count() == 0 )
currentRow = -1;
}
} */
void QuickpostTemplateDialog::on_pbOK_clicked()
{
//qDebug( "Accept clicked" );
//on_pbAccept_clicked();
if( doingNewTemplate && !dirty ) {
_templateTitles.removeAt( currentRow );
_templateStrings.removeAt( currentRow );
_defaultPublishStates.removeAt( currentRow );
_assocHostStrings.removeAt( currentRow );
_copyTitleStates.removeAt( currentRow );
}
// We need to get rid of any empty or whitespace-only associated hosts entries
QRegExp emptyString( "^\\s+$" );
int i, j;
_assocHostLists = compileAssocHostLists( _assocHostStrings );
if( _assocHostLists.count() ) {
for( i = 0; i < _assocHostLists.count(); i++ ) {
for( j = 0; j < _assocHostLists[i].count(); j++ ) {
if( _assocHostLists[i].at( j ).isEmpty() ||
emptyString.exactMatch( _assocHostLists[i].at( j ) ) )
_assocHostLists[i].removeAt( j );
}
}
}
accept();
}
void QuickpostTemplateDialog::on_leName_textEdited( const QString &newName )
{
if( currentRow != -1 ) {
// qDebug( "title edited" );
lwTemplates->item( currentRow )->setText( newName.isEmpty() ?
tr( "(No name)" ) : newName );
_templateTitles[currentRow] = newName;
}
}
void QuickpostTemplateDialog::on_teTemplateText_textChanged()
{
if( currentRow != -1 ) {
_templateStrings[currentRow] = teTemplateText->toPlainText();
}
}
void QuickpostTemplateDialog::on_teAssocHosts_textChanged()
{
if( currentRow != -1 ) {
_assocHostStrings[currentRow] = teAssocHosts->toPlainText();
}
}
void QuickpostTemplateDialog::on_cbDefaultPublishStatus_currentIndexChanged( int newState )
{
if( currentRow != -1 )
_defaultPublishStates[currentRow] = newState;
}
void QuickpostTemplateDialog::on_chCopyTitle_stateChanged( int )
{
if( currentRow != -1 )
_copyTitleStates[currentRow] = chCopyTitle->isChecked();
}
QList<QStringList> QuickpostTemplateDialog::assocHostLists()
{
int i, j;
QRegExp emptyString( "^\\s+$" );
_assocHostLists = compileAssocHostLists( _assocHostStrings );
if( _assocHostLists.count() ) {
for( i = 0; i < _assocHostLists.count(); i++ ) {
for( j = 0; j < _assocHostLists[i].count(); j++ ) {
if( _assocHostLists[i].at( j ).isEmpty() ||
emptyString.exactMatch( _assocHostLists[i].at( j ) ) )
_assocHostLists[i].removeAt( j );
}
}
}
return _assocHostLists;
}
QList<QStringList> QuickpostTemplateDialog::compileAssocHostLists( QStringList &input )
{
QList<QStringList> rv;
Q_FOREACH( QString a, input ) {
rv.append( a.split( "\n" ) );
}
return rv;
}
QStringList QuickpostTemplateDialog::compileAssocHostStrings( QList<QStringList> &input )
{
QStringList rv;
Q_FOREACH( QStringList a, input ) {
rv.append( a.join( "\n" ) );
}
return rv;
}
|