| 1 | // |
| 2 | // C++ Implementation: PhpGalleryUtil |
| 3 | // |
| 4 | // Description: |
| 5 | // |
| 6 | // |
| 7 | // Author: mythtv <mythtv@jshoor-media>, (C) 2007 |
| 8 | // |
| 9 | // Copyright: See COPYING file that comes with this distribution |
| 10 | // |
| 11 | // |
| 12 | |
| 13 | #include <qdir.h> |
| 14 | #include <qhbox.h> |
| 15 | #include <qvbox.h> |
| 16 | #include <qapplication.h> |
| 17 | #include <mythtv/mythcontext.h> |
| 18 | #include <mythtv/mythwidgets.h> |
| 19 | #include <mythtv/lcddevice.h> |
| 20 | |
| 21 | #include "phpgalleryutil.h" |
| 22 | |
| 23 | QString PhpGalleryUtil::m_startURL; |
| 24 | QString PhpGalleryUtil::m_startDir; |
| 25 | Album *PhpGalleryUtil::m_rootAlbum; |
| 26 | int PhpGalleryUtil::m_refreshCache; |
| 27 | QPtrList<Album> PhpGalleryUtil::m_all_albums; |
| 28 | QDict<Album> PhpGalleryUtil::m_temp_album_dict; |
| 29 | QDict<Album> PhpGalleryUtil::m_album_dict; |
| 30 | int PhpGalleryUtil::m_progress; |
| 31 | |
| 32 | |
| 33 | PhpGalleryUtil::PhpGalleryUtil() |
| 34 | { |
| 35 | m_startURL = gContext->GetSetting("PHPGalleryURL"); |
| 36 | m_startDir = gContext->GetSetting("GalleryDir"); |
| 37 | m_refreshCache = gContext->GetNumSetting("PHPGalleryRefreshCache"); |
| 38 | m_progress = 0; |
| 39 | m_stopped = false; |
| 40 | |
| 41 | QDir dir(m_startDir); |
| 42 | if (!dir.exists() || !dir.isReadable()) |
| 43 | { |
| 44 | m_errorStr = tr("MythGallery Directory '%1' does not exist " |
| 45 | "or is unreadable.").arg(m_startDir); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | QString response = MakeRequest("fetch-albums&g2_form%5Bno_perms%5D=yes"); |
| 50 | if (!response){ |
| 51 | m_errorStr += tr("Php Gallery URL '%1' is down or is not correct.").arg(m_startURL); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | QStringList responseLines = QStringList::split("\n",response); |
| 56 | QString albumNumber = NULL; |
| 57 | for ( QStringList::Iterator it = responseLines.begin(); it != responseLines.end(); ++it ) |
| 58 | { |
| 59 | QStringList nameList; |
| 60 | QString propValue; |
| 61 | ReadResponseLine(*it, nameList, propValue); |
| 62 | if ((nameList[0].compare("album")==0)&&((nameList.last()).compare(albumNumber)!=0)) |
| 63 | { |
| 64 | QString albumName=NULL; |
| 65 | QString albumTitle=NULL; |
| 66 | QString albumParent=NULL; |
| 67 | albumNumber = nameList.last(); |
| 68 | if (nameList[1].compare("name")==0) |
| 69 | { |
| 70 | albumName = propValue; |
| 71 | } |
| 72 | ReadResponseLine(*++it, nameList, propValue); |
| 73 | if ((nameList[1].compare("title")==0)&&((nameList.last()).compare(albumNumber)==0)) |
| 74 | { |
| 75 | propValue.replace("/", "-" ); |
| 76 | propValue.replace("\\", "-" ); |
| 77 | albumTitle = propValue; |
| 78 | } |
| 79 | it++; |
| 80 | ReadResponseLine(*++it, nameList, propValue); |
| 81 | if ((nameList[1].compare("parent")==0)&&((nameList.last()).compare(albumNumber)==0)) |
| 82 | { |
| 83 | albumParent = propValue; |
| 84 | } |
| 85 | if (albumParent.compare("0")==0){ |
| 86 | m_rootAlbum = new Album(albumName, albumTitle, albumParent); |
| 87 | m_rootAlbum->SetPath(m_startDir + "/"); |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | Album *album = new Album(albumName, albumTitle, albumParent); |
| 92 | m_all_albums.append(album); |
| 93 | m_temp_album_dict.insert(album->GetName(),album); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | QPtrListIterator<Album> it( m_all_albums ); |
| 100 | Album *thisalbum; |
| 101 | |
| 102 | while ((thisalbum = it.current()) != 0){ |
| 103 | thisalbum->SetPath(BuildPath(thisalbum,thisalbum->GetTitle())); |
| 104 | m_album_dict.insert(thisalbum->GetTitle(),thisalbum); |
| 105 | ++it; |
| 106 | } |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | PhpGalleryUtil::~PhpGalleryUtil() |
| 111 | { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | QString PhpGalleryUtil::BuildPath(Album *album, QString path) |
| 116 | { |
| 117 | if (album->GetParent().compare(m_rootAlbum->GetName())==0) |
| 118 | { |
| 119 | path.prepend(m_startDir + "/"); |
| 120 | QDir dir(path); |
| 121 | if (!dir.exists()) |
| 122 | { |
| 123 | dir.mkdir(path,true); |
| 124 | } |
| 125 | return path; |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | path.prepend(m_temp_album_dict.find(album->GetParent())->GetTitle()+"/"); |
| 130 | return BuildPath(m_temp_album_dict.find(album->GetParent()),path); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | void PhpGalleryUtil::ReadResponseLine(QString responseLine, QStringList &nameList, QString &propValue) |
| 136 | { |
| 137 | QStringList propertyMap = QStringList::split("=", responseLine); |
| 138 | QString propName = propertyMap[0]; |
| 139 | propValue = propertyMap[1]; |
| 140 | nameList = QStringList::split(".", propName); |
| 141 | } |
| 142 | |
| 143 | void PhpGalleryUtil::LoadAll(const QString& dir, MythPhpDownloadProgressDialog *dialog) |
| 144 | { |
| 145 | Album *album; |
| 146 | if (dir.compare(m_startDir)==0) |
| 147 | { |
| 148 | album = m_rootAlbum; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | album = m_album_dict.find(dir.section("/",-1)); |
| 153 | } |
| 154 | GetAll(album, dialog); |
| 155 | dialog->setPrimaryProgress(++m_progress); |
| 156 | QPtrListIterator<Album> it( m_all_albums ); |
| 157 | Album *thisalbum; |
| 158 | |
| 159 | while ((thisalbum = it.current()) != 0) |
| 160 | { |
| 161 | if (m_stopped) |
| 162 | return; |
| 163 | if (thisalbum->GetParent().compare(album->GetName()) == 0) |
| 164 | { |
| 165 | LoadAll(thisalbum->GetPath(), dialog); |
| 166 | } |
| 167 | ++it; |
| 168 | } |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | void PhpGalleryUtil::GetAll(Album *album, MythPhpDownloadProgressDialog *dialog) |
| 173 | { |
| 174 | int progress = 1; |
| 175 | dialog->setSecondaryLabel(album->GetTitle()); |
| 176 | QString response = MakeRequest("fetch-album-images&g2_form%5Bset_albumName%5D=" + album->GetName()); |
| 177 | QStringList responseLines = QStringList::split("\n",response); |
| 178 | |
| 179 | //Find total items in album |
| 180 | for ( QStringList::Iterator it = --responseLines.end(); it != responseLines.begin(); --it ) |
| 181 | { |
| 182 | if (m_stopped) |
| 183 | return; |
| 184 | QStringList nameList; |
| 185 | QString propValue; |
| 186 | ReadResponseLine(*it, nameList, propValue); |
| 187 | if (nameList[0].compare("image_count")==0) |
| 188 | { |
| 189 | dialog->setSecondaryTotalSteps(propValue.toInt()); |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | //Download items in album |
| 195 | QString itemNumber = NULL; |
| 196 | for ( QStringList::Iterator it = responseLines.begin(); it != responseLines.end(); ++it ) |
| 197 | { |
| 198 | if (m_stopped) |
| 199 | return; |
| 200 | QStringList nameList; |
| 201 | QString propValue; |
| 202 | ReadResponseLine(*it, nameList, propValue); |
| 203 | if ((nameList[0].compare("image")==0)&&(nameList.last().compare(itemNumber)!=0)) |
| 204 | { |
| 205 | |
| 206 | /* image.name.1=12227 |
| 207 | image.raw_width.1=3008 |
| 208 | image.raw_height.1=2000 |
| 209 | image.raw_filesize.1=1196696 |
| 210 | image.resizedName.1=12228 if exists |
| 211 | image.resized_width.1=640 if exists |
| 212 | image.resized_height.1=426 if exists |
| 213 | image.thumbName.1=12229 |
| 214 | image.thumb_width.1=150 |
| 215 | image.thumb_height.1=100 |
| 216 | image.forceExtension.1=jpg |
| 217 | image.caption.1=DSC_0005.JPG |
| 218 | image.title.1=DSC_0005.JPG |
| 219 | image.hidden.1=no*/ |
| 220 | |
| 221 | QString itemName=NULL; |
| 222 | QString thumbName=NULL; |
| 223 | QString itemTitle=NULL; |
| 224 | QString itemParent=NULL; |
| 225 | QString itemExtension=NULL; |
| 226 | |
| 227 | itemNumber = nameList.last(); |
| 228 | if (nameList[1].compare("name")==0) |
| 229 | { |
| 230 | itemName = propValue; |
| 231 | } |
| 232 | it+=4; |
| 233 | ReadResponseLine(*it, nameList, propValue); |
| 234 | if (nameList[1].compare("resizedName")==0) |
| 235 | { |
| 236 | itemName = propValue; |
| 237 | it+=3; |
| 238 | } |
| 239 | |
| 240 | ReadResponseLine(*it, nameList, propValue); |
| 241 | if (nameList[1].compare("thumbName")==0) |
| 242 | { |
| 243 | thumbName = propValue; |
| 244 | } |
| 245 | it+=2; |
| 246 | ReadResponseLine(*++it, nameList, propValue); |
| 247 | if ((nameList[1].compare("forceExtension")==0)&&(nameList.last().compare(itemNumber)==0)) |
| 248 | { |
| 249 | itemExtension = propValue; |
| 250 | } |
| 251 | ++it; |
| 252 | ReadResponseLine(*++it, nameList, propValue); |
| 253 | if ((nameList[1].compare("title")==0)&&(nameList.last().compare(itemNumber)==0)) |
| 254 | { |
| 255 | itemTitle = propValue; |
| 256 | } |
| 257 | //make and set QPixmap |
| 258 | QDir dir(album->GetPath()); |
| 259 | if (!dir.exists()) |
| 260 | dir.mkdir(album->GetPath()); |
| 261 | |
| 262 | float wmult, hmult; |
| 263 | int screenwidth, screenheight; |
| 264 | int xbase, ybase; |
| 265 | gContext->GetScreenSettings(xbase, screenwidth, wmult, ybase, screenheight, hmult); |
| 266 | |
| 267 | //Download image |
| 268 | QString tempFile= album->GetPath() + "/" + itemTitle; |
| 269 | QString url = gContext->GetSetting("PHPGalleryURL")+"?g2_view=core.DownloadItem&g2_itemId="+itemName; |
| 270 | if (!QFile::exists(tempFile) || m_refreshCache) |
| 271 | { |
| 272 | if (!HttpComms::getHttpFile(tempFile, url, 20000, 3)) |
| 273 | { |
| 274 | VERBOSE(VB_IMPORTANT, QString("Download Image failed")); |
| 275 | } |
| 276 | VERBOSE(VB_NETWORK, QString("Downloading Image '%1'").arg(tempFile)); |
| 277 | } |
| 278 | //Scale Image |
| 279 | QImage image = QImage(tempFile); |
| 280 | if (!image.isNull()) |
| 281 | { |
| 282 | image.smoothScale(screenwidth,screenheight,QImage::ScaleMin); |
| 283 | image.save(tempFile, "JPEG"); |
| 284 | } |
| 285 | dialog->setSecondaryProgress(progress++); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | QString PhpGalleryUtil::MakeRequest(QString cmd){ |
| 293 | QUrl url = m_startURL; |
| 294 | |
| 295 | // Make Request to server |
| 296 | QByteArray aBuffer; |
| 297 | QTextStream os( aBuffer, IO_WriteOnly ); |
| 298 | os << "g2_controller=remote%3AGalleryRemote&protocol_version=2.11&g2_form%5Bcmd%5D="; |
| 299 | os << cmd; |
| 300 | QBuffer buff( aBuffer ); |
| 301 | QHttpRequestHeader header; |
| 302 | |
| 303 | header.setValue("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint,application/msword, application/x-shockwave-flash, */*" ); |
| 304 | header.setValue("Accept-Language", "en-us"); |
| 305 | header.setValue("Content-Type", "application/x-www-form-urlencoded"); |
| 306 | header.setValue("Connection", "Keep-Alive"); |
| 307 | header.setValue("Cache-Control", "no-cache"); |
| 308 | |
| 309 | QString response = HttpComms::postHttp( url, |
| 310 | &header, |
| 311 | &buff, |
| 312 | 20000, // ms |
| 313 | 3, // retries |
| 314 | 0, // redirects |
| 315 | false, // allow gzip |
| 316 | NULL, // login |
| 317 | true ); |
| 318 | return response; |
| 319 | |
| 320 | } |
| 321 | |
| 322 | Album::Album(QString albumName, QString albumTitle, QString albumParent){ |
| 323 | m_name = albumName; |
| 324 | m_title = albumTitle; |
| 325 | m_parent = albumParent; |
| 326 | } |
| 327 | |
| 328 | MythPhpDownloadProgressDialog::MythPhpDownloadProgressDialog(const QString &message, int primaryTotalSteps, int secondaryTotalSteps, |
| 329 | bool cancelButton, const QObject *target, const char *slot) |
| 330 | : MythDialog(gContext->GetMainWindow(), "progress", false) |
| 331 | { |
| 332 | int screenwidth, screenheight; |
| 333 | float wmult, hmult; |
| 334 | |
| 335 | gContext->GetScreenSettings(screenwidth, wmult, screenheight, hmult); |
| 336 | |
| 337 | setFont(gContext->GetMediumFont()); |
| 338 | |
| 339 | gContext->ThemeWidget(this); |
| 340 | |
| 341 | int yoff = screenheight / 3; |
| 342 | int xoff = screenwidth / 10; |
| 343 | setGeometry(xoff, yoff, screenwidth - xoff * 2, yoff); |
| 344 | setFixedSize(QSize(screenwidth - xoff * 2, yoff)); |
| 345 | |
| 346 | QVBoxLayout *lay = new QVBoxLayout(this, 0); |
| 347 | |
| 348 | QVBox *vbox = new QVBox(this); |
| 349 | lay->addWidget(vbox); |
| 350 | |
| 351 | vbox->setLineWidth(3); |
| 352 | vbox->setMidLineWidth(3); |
| 353 | vbox->setFrameShape(QFrame::Panel); |
| 354 | vbox->setFrameShadow(QFrame::Raised); |
| 355 | vbox->setMargin((int)(15 * wmult)); |
| 356 | |
| 357 | primarymsglabel = new QLabel(vbox); |
| 358 | primarymsglabel->setBackgroundOrigin(ParentOrigin); |
| 359 | primarymsglabel->setText(message); |
| 360 | vbox->setStretchFactor(primarymsglabel, 5); |
| 361 | |
| 362 | QHBox *hbox = new QHBox(vbox); |
| 363 | hbox->setSpacing(5); |
| 364 | |
| 365 | primaryProgress = new QProgressBar(primaryTotalSteps, hbox); |
| 366 | primaryProgress->setBackgroundOrigin(ParentOrigin); |
| 367 | |
| 368 | secondarymsglabel = new QLabel(vbox); |
| 369 | secondarymsglabel->setBackgroundOrigin(ParentOrigin); |
| 370 | vbox->setStretchFactor(secondarymsglabel, 5); |
| 371 | QHBox *shbox = new QHBox(vbox); |
| 372 | shbox->setSpacing(5); |
| 373 | |
| 374 | secondaryProgress = new QProgressBar(secondaryTotalSteps, shbox); |
| 375 | secondaryProgress->setBackgroundOrigin(ParentOrigin); |
| 376 | |
| 377 | if (cancelButton && slot && target) |
| 378 | { |
| 379 | MythPushButton *button = new MythPushButton("Cancel", vbox, 0); |
| 380 | button->setFocus(); |
| 381 | connect(button, SIGNAL(pressed()), target, slot); |
| 382 | } |
| 383 | |
| 384 | setPrimaryTotalSteps(primaryTotalSteps); |
| 385 | setSecondaryTotalSteps(secondaryTotalSteps); |
| 386 | |
| 387 | if (class LCD * lcddev = LCD::Get()) |
| 388 | { |
| 389 | textItems = new QPtrList<LCDTextItem>; |
| 390 | textItems->setAutoDelete(true); |
| 391 | |
| 392 | textItems->clear(); |
| 393 | textItems->append(new LCDTextItem(1, ALIGN_CENTERED, message, "Generic", |
| 394 | false)); |
| 395 | lcddev->switchToGeneric(textItems); |
| 396 | } |
| 397 | else |
| 398 | textItems = NULL; |
| 399 | |
| 400 | show(); |
| 401 | |
| 402 | qApp->processEvents(); |
| 403 | } |
| 404 | |
| 405 | MythPhpDownloadProgressDialog::~MythPhpDownloadProgressDialog() |
| 406 | { |
| 407 | Teardown(); |
| 408 | } |
| 409 | |
| 410 | void MythPhpDownloadProgressDialog::deleteLater(void) |
| 411 | { |
| 412 | hide(); |
| 413 | Teardown(); |
| 414 | MythDialog::deleteLater(); |
| 415 | } |
| 416 | |
| 417 | void MythPhpDownloadProgressDialog::Teardown(void) |
| 418 | { |
| 419 | if (textItems) |
| 420 | { |
| 421 | delete textItems; |
| 422 | textItems = NULL; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | void MythPhpDownloadProgressDialog::Close(void) |
| 427 | { |
| 428 | accept(); |
| 429 | |
| 430 | LCD *lcddev = LCD::Get(); |
| 431 | if (lcddev) |
| 432 | { |
| 433 | lcddev->switchToNothing(); |
| 434 | lcddev->switchToTime(); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | void MythPhpDownloadProgressDialog::setPrimaryProgress(int curprogress) |
| 439 | { |
| 440 | primaryProgress->setProgress(curprogress); |
| 441 | if (curprogress % primarySteps == 0) |
| 442 | { |
| 443 | qApp->processEvents(); |
| 444 | if (LCD * lcddev = LCD::Get()) |
| 445 | { |
| 446 | float fProgress = (float)curprogress / m_primaryTotalSteps; |
| 447 | lcddev->setGenericProgress(fProgress); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | void MythPhpDownloadProgressDialog::setSecondaryProgress(int curprogress) |
| 453 | { |
| 454 | secondaryProgress->setProgress(curprogress); |
| 455 | if (curprogress % secondarySteps == 0) |
| 456 | { |
| 457 | qApp->processEvents(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void MythPhpDownloadProgressDialog::setPrimaryLabel(QString newlabel) |
| 462 | { |
| 463 | primarymsglabel->setText(newlabel); |
| 464 | } |
| 465 | |
| 466 | void MythPhpDownloadProgressDialog::setSecondaryLabel(QString newlabel) |
| 467 | { |
| 468 | secondarymsglabel->setText(newlabel); |
| 469 | } |
| 470 | |
| 471 | void MythPhpDownloadProgressDialog::keyPressEvent(QKeyEvent *e) |
| 472 | { |
| 473 | bool handled = false; |
| 474 | QStringList actions; |
| 475 | if (gContext->GetMainWindow()->TranslateKeyPress("qt", e, actions)) |
| 476 | { |
| 477 | for (unsigned int i = 0; i < actions.size() && !handled; i++) |
| 478 | { |
| 479 | QString action = actions[i]; |
| 480 | if (action == "ESCAPE") |
| 481 | handled = true; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | if (!handled) |
| 486 | MythDialog::keyPressEvent(e); |
| 487 | } |
| 488 | |
| 489 | void MythPhpDownloadProgressDialog::setPrimaryTotalSteps(int totalSteps) |
| 490 | { |
| 491 | m_primaryTotalSteps = totalSteps; |
| 492 | primaryProgress->setTotalSteps(totalSteps); |
| 493 | primarySteps = totalSteps / 1000; |
| 494 | if (primarySteps == 0) |
| 495 | primarySteps = 1; |
| 496 | } |
| 497 | |
| 498 | void MythPhpDownloadProgressDialog::setSecondaryTotalSteps(int totalSteps) |
| 499 | { |
| 500 | m_secondaryTotalSteps = totalSteps; |
| 501 | secondaryProgress->setTotalSteps(totalSteps); |
| 502 | secondarySteps = totalSteps / 1000; |
| 503 | if (secondarySteps == 0) |
| 504 | secondarySteps = 1; |
| 505 | } |