/* Copyright 2003 Rikard Björklind, Mikael Gransell This file is part of dc-qt. dc-qt is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. dc-qt 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 dc-qt; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "util.h" #include "dc_settings.h" #include "debugdlg.h" #include #include #include #include #include #include #include #include #include #include "FileExistsDlg.h" #include using namespace std; QString bytes_to_units(const QString &s) { return bytes_to_units(s.toDouble()); } QString bytes_to_units(double sb) { QString t; if (sb > 1000000000000.0) t.sprintf("%.2lfTB", sb / 1000000000000.0); else if (sb > 1000000000.0) t.sprintf("%.2lfGB", sb / 1000000000.0); else if (sb > 1000000.0) t.sprintf("%.2lfMB", sb / 1000000.0); else if (sb > 1000.0) t.sprintf("%.2lfKB", sb / 1000.0); else //t.sprintf("%.2lfBT", sb); t.sprintf("%d",(int)sb); return t; } double units_to_bytes(const QString &s) { QString num(s.left(s.length() - 2)); QString units(s.right(2)); bool ok; double ret = num.toDouble(&ok); assert(ok); if (units == "KB") // ret *= 1000.0; ret*=1024.0; else if (units == "MB") // ret *= 1000000.0; ret*=1024.0*1024.0; else if (units == "GB") // ret *= 1000000000.0; ret*=1024.0*1024.0*1024.0; else if (units == "TB") // ret *= 1000000000000.0; ret *= 1024.0*1024.0*1024.0*1024.0; return ret; } QString sec_to_time(float seconds) { if (seconds <= 0) return "0:0:0"; // we can be passed in seconds=inf if (seconds >= 3600 * 24 * 2) return "> 2 days"; int hours = 0, min = 0; while (seconds >= 3600) { hours++; seconds -= 3600; } while (seconds >= 60) { min++; seconds -= 60; } QString result; result.sprintf("%d:%02d:%02d", hours, min, int(seconds + 0.5)); return result; } /// Checks the validity of the input file and returns the local filename without full path. QString checkFile(const QString& file,int size) { QString targetFile = settings.get_setting("dl_dir") + file.section('\\',-1,-1); debugWin->print("checkFile() targetfile=\n" + targetFile+"\n"); QFileInfo finfo( targetFile ); QFile f( targetFile ); // If the target file exists, ask what to do if( finfo.exists() ) { debugWin->print("Exists!\n"); FileExistsDlg *dlg = new FileExistsDlg(finfo,size); while(1) { dlg->exec(); switch( dlg->Action() ) { case FileExistsDlg::OVERWRITE: debugWin->print("Removing file " + f.name() + "\n"); if(!f.remove()) { QMessageBox::critical(0, "dc-qt: Error removing file", "The file could not be deleted", QMessageBox::Ok, QMessageBox::NoButton); return QString::null; } else return finfo.fileName(); break; case FileExistsDlg::RENAME: finfo.setFile(dlg->NewName()); f.setName(dlg->NewName()); if(!finfo.exists()) return dlg->NewName(); break; case FileExistsDlg::SKIP: debugWin->print("Action is SKIP\n"); return QString::null; break; default: debugWin->print("Fett error\n"); } } } return file.section('\\',-1,-1); } QString selectDlDirDlg() { static QString dir = settings.get_setting("alt_dldir"); if (dir.isEmpty()) { dir = settings.get_setting("dl_dir"); if (dir.isEmpty()) dir = settings.get_home_dir(); } dir = QFileDialog::getExistingDirectory(dir, 0, "getdldirdlg", "Select download directory"); if (!dir.isEmpty() && dir.at(dir.length()-1)!='/' && dir.at(dir.length()-1)!='\\') dir = dir+'/'; settings.set_setting("alt_dldir", dir); settings.save_to_file(); return dir; } // help methods to set/get widths of columns of a listview, where // widths are being stored in an intlist_setting bool set_lv_widths(QListView *lv, const intlist_setting &widths) { if (lv->columns() != widths.size) return false; QValueList::const_iterator it = widths.intlist.begin(); for (int c=0; csetColumnWidthMode(c, QListView::Manual); lv->setColumnWidth(c, (*it)); it++; } return true; } void get_lv_widths(const QListView *lv, intlist_setting *widths) { widths->size=0; widths->intlist.clear(); for (int c=0; ccolumns(); c++) { widths->intlist.push_back(lv->columnWidth(c)); widths->size++; } } intlist_setting::intlist_setting(QValueList &l) : intlist(l), size(l.count()) { } intlist_setting::intlist_setting(QString s) { bool more = !s.isEmpty(); size = 0; while (more) { int v = (s.section(',', size,size)).toInt(&more); if (more && v>=0) { intlist.push_back(v); size++; } else more = false; } } QString intlist_setting::toString() { QString res(""); QValueList::iterator ilv = intlist.begin(); for (int i=0; i::iterator i = intlist.begin(); int x = *i; i++; int y = *i; i++; int w = *i; i++; int h = *i; return (x>=0 && y>=0 && w>0 && h>0); } }