Changeset 14204 for trunk/qt/favicon.cc


Ignore:
Timestamp:
Sep 14, 2013, 10:45:04 PM (9 years ago)
Author:
jordan
Message:

copyediting: indentation cleanups

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/favicon.cc

    r14163 r14204  
    2828***/
    2929
    30 Favicons :: Favicons( )
     30Favicons :: Favicons ()
    3131{
    32     myNAM = new QNetworkAccessManager( );
    33     connect( myNAM, SIGNAL(finished(QNetworkReply*)), this, SLOT(onRequestFinished(QNetworkReply*)) );
     32  myNAM = new QNetworkAccessManager ();
     33  connect (myNAM, SIGNAL(finished(QNetworkReply*)), this, SLOT(onRequestFinished(QNetworkReply*)));
    3434}
    3535
    36 Favicons :: ~Favicons( )
     36Favicons :: ~Favicons ()
    3737{
    38     delete myNAM;
     38  delete myNAM;
    3939}
    4040
     
    4444
    4545QString
    46 Favicons :: getCacheDir( )
     46Favicons :: getCacheDir ()
    4747{
    48     const QString base =
     48  const QString base =
    4949#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
    50         QDesktopServices::storageLocation( QDesktopServices::CacheLocation );
     50    QDesktopServices::storageLocation (QDesktopServices::CacheLocation);
    5151#else
    52         QStandardPaths::writableLocation( QStandardPaths::CacheLocation );
     52    QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
    5353#endif
    5454
    55     return QDir( base ).absoluteFilePath( "favicons" );
     55  return QDir(base).absoluteFilePath ("favicons");
    5656}
    5757
    5858void
    59 Favicons :: ensureCacheDirHasBeenScanned( )
     59Favicons :: ensureCacheDirHasBeenScanned ()
    6060{
    61     static bool hasBeenScanned = false;
     61  static bool hasBeenScanned = false;
    6262
    63     if( !hasBeenScanned )
     63  if (!hasBeenScanned)
    6464    {
    65         hasBeenScanned = true;
     65      hasBeenScanned = true;
    6666
    67         QDir cacheDir( getCacheDir( ) );
    68         cacheDir.mkpath( cacheDir.absolutePath( ) );
     67      QDir cacheDir (getCacheDir ());
     68      cacheDir.mkpath (cacheDir.absolutePath ());
    6969
    70         QStringList files = cacheDir.entryList( QDir::Files|QDir::Readable );
    71         foreach( QString file, files ) {
    72             QPixmap pixmap;
    73             pixmap.load( cacheDir.absoluteFilePath( file ) );
    74             if( !pixmap.isNull( ) )
    75                 myPixmaps.insert( file, pixmap );
     70      QStringList files = cacheDir.entryList (QDir::Files|QDir::Readable);
     71      foreach (QString file, files)
     72        {
     73          QPixmap pixmap;
     74          pixmap.load (cacheDir.absoluteFilePath (file));
     75          if (!pixmap.isNull ())
     76            myPixmaps.insert (file, pixmap);
    7677        }
    7778    }
     
    7980
    8081QString
    81 Favicons :: getHost( const QUrl& url )
     82Favicons :: getHost (const QUrl& url)
    8283{
    83     QString host = url.host( );
    84     const int first_dot = host.indexOf( '.' );
    85     const int last_dot = host.lastIndexOf( '.' );
     84  QString host = url.host ();
     85  const int first_dot = host.indexOf ('.');
     86  const int last_dot = host.lastIndexOf ('.');
    8687
    87     if( ( first_dot != -1 ) && ( last_dot != -1 ) && ( first_dot != last_dot ) )
    88         host.remove( 0, first_dot + 1 );
     88  if ((first_dot != -1) && (last_dot != -1) &&  (first_dot != last_dot))
     89    host.remove (0, first_dot + 1);
    8990
    90     return host;
     91  return host;
    9192}
    9293
    9394QPixmap
    94 Favicons :: find( const QUrl& url )
     95Favicons :: find (const QUrl& url)
    9596{
    96     return findFromHost( getHost( url ) );
     97  return findFromHost (getHost (url));
    9798}
    9899
    99100namespace
    100101{
    101     const QSize rightSize( 16, 16 );
     102  const QSize rightSize (16, 16);
    102103};
    103104
    104105QPixmap
    105 Favicons :: findFromHost( const QString& host )
     106Favicons :: findFromHost (const QString& host)
    106107{
    107     ensureCacheDirHasBeenScanned( );
     108  ensureCacheDirHasBeenScanned ();
    108109
    109     const QPixmap pixmap = myPixmaps[ host ];
    110     return pixmap.size()==rightSize ? pixmap : pixmap.scaled(rightSize);
     110  const QPixmap pixmap = myPixmaps[ host ];
     111  return pixmap.size()==rightSize ? pixmap : pixmap.scaled(rightSize);
    111112}
    112113
    113114void
    114 Favicons :: add( const QUrl& url )
     115Favicons :: add (const QUrl& url)
    115116{
    116     ensureCacheDirHasBeenScanned( );
     117  ensureCacheDirHasBeenScanned ();
    117118
    118     const QString host = getHost( url );
     119  const QString host = getHost (url);
    119120
    120     if( !myPixmaps.contains( host ) )
     121  if (!myPixmaps.contains (host))
    121122    {
    122         // add a placholder s.t. we only ping the server once per session
    123         QPixmap tmp( rightSize );
    124         tmp.fill( Qt::transparent );
    125         myPixmaps.insert( host, tmp );
     123      // add a placholder s.t. we only ping the server once per session
     124      QPixmap tmp (rightSize);
     125      tmp.fill (Qt::transparent);
     126      myPixmaps.insert (host, tmp);
    126127
    127         // try to download the favicon
    128         const QString path = "http://" + host + "/favicon.";
    129         QStringList suffixes;
    130         suffixes << "ico" << "png" << "gif" << "jpg";
    131         foreach( QString suffix, suffixes )
    132             myNAM->get( QNetworkRequest( path + suffix ) );
     128      // try to download the favicon
     129      const QString path = "http://" + host + "/favicon.";
     130      QStringList suffixes;
     131      suffixes << "ico" << "png" << "gif" << "jpg";
     132      foreach (QString suffix, suffixes)
     133        myNAM->get (QNetworkRequest (path + suffix));
    133134    }
    134135}
    135136
    136137void
    137 Favicons :: onRequestFinished( QNetworkReply * reply )
     138Favicons :: onRequestFinished (QNetworkReply * reply)
    138139{
    139     const QString host = reply->url().host();
     140  const QString host = reply->url().host();
    140141
    141     QPixmap pixmap;
     142  QPixmap pixmap;
    142143
    143     const QByteArray content = reply->readAll( );
    144     if( !reply->error( ) )
    145         pixmap.loadFromData( content );
     144  const QByteArray content = reply->readAll ();
     145  if (!reply->error ())
     146    pixmap.loadFromData (content);
    146147
    147     if( !pixmap.isNull( ) )
     148  if (!pixmap.isNull ())
    148149    {
    149         // save it in memory...
    150         myPixmaps.insert( host, pixmap );
     150      // save it in memory...
     151      myPixmaps.insert (host, pixmap);
    151152
    152         // save it on disk...
    153         QDir cacheDir( getCacheDir( ) );
    154         cacheDir.mkpath( cacheDir.absolutePath( ) );
    155         QFile file( cacheDir.absoluteFilePath( host ) );
    156         file.open( QIODevice::WriteOnly );
    157         file.write( content );
    158         file.close( );
     153      // save it on disk...
     154      QDir cacheDir (getCacheDir ());
     155      cacheDir.mkpath (cacheDir.absolutePath ());
     156      QFile file (cacheDir.absoluteFilePath (host));
     157      file.open (QIODevice::WriteOnly);
     158      file.write (content);
     159      file.close ();
    159160
    160         // notify listeners
    161         emit pixmapReady( host );
     161      // notify listeners
     162      emit pixmapReady (host);
    162163    }
    163164}
Note: See TracChangeset for help on using the changeset viewer.