1 /***********************************************************************
5 **********************************************************************/
14 #include <sys/types.h>
20 #include <QtCore/QTextStream>
21 #include <QtCore/QFileInfo>
22 #include <QtCore/QDir>
23 #include <QtGui/QClipboard>
24 #include <QtGui/QPixmap>
25 #include <Qt3Support/Q3UriDrag>
26 #include <QtGui/QTextDocument>
27 #include <Qt3Support/Q3PtrList>
28 #include <QtCore/QDate>
30 #include <kfiledialog.h>
32 #include <kapplication.h>
34 #include <kpropertiesdialog.h>
35 #include <kstandarddirs.h>
36 #include <kmessagebox.h>
37 #include <kmimetype.h>
40 #include <kio/netaccess.h>
41 #include <k3urldrag.h>
43 #include <kiconloader.h>
45 template class Q3PtrList
<KfFileLVI
>;
48 static const char* perm
[4] = {
49 I18N_NOOP( "Read-write" ),
50 I18N_NOOP( "Read-only" ),
51 I18N_NOOP( "Write-only" ),
52 I18N_NOOP( "Inaccessible" ) };
58 KfFileLVI::KfFileLVI(KfindWindow
* lv
, const KFileItem
&item
, const QString
& matchingLine
)
62 fileInfo
= new QFileInfo(item
.url().path());
64 QString size
= KGlobal::locale()->formatNumber(item
.size(), 0);
66 QString date
= item
.timeString(KFileItem::ModificationTime
);
69 if(fileInfo
->isReadable())
70 perm_index
= fileInfo
->isWritable() ? RW
: RO
;
72 perm_index
= fileInfo
->isWritable() ? WO
: NA
;
74 // Fill the item with data
75 setText(0, item
.url().fileName(KUrl::ObeyTrailingSlash
));
76 setText(1, lv
->reducedDir(item
.url().directory(KUrl::AppendTrailingSlash
)));
79 setText(4, i18n(perm
[perm_index
]));
80 setText(5, matchingLine
);
82 // put the icon into the leftmost column
83 setPixmap(0, item
.pixmap(16));
86 KfFileLVI::~KfFileLVI()
91 QString
KfFileLVI::key(int column
, bool) const
95 // Returns size in bytes. Used for sorting
96 return QString().sprintf("%010ld", (long int)fileInfo
->size());
99 unsigned long l_time
= fileitem
.time(KFileItem::ModificationTime
).toTime_t();
100 // Returns time in secs from 1/1/1970. Used for sorting
101 return QString().sprintf("%010lu", l_time
);
108 KfindWindow::KfindWindow( QWidget
*parent
)
109 : K3ListView( parent
)
113 setSelectionMode( Q3ListView::Extended
);
114 setShowSortIndicator( true );
116 addColumn(i18n("Name"));
117 addColumn(i18n("In Subfolder"));
118 addColumn(i18n("Size"));
119 setColumnAlignment(2, Qt::AlignRight
);
120 addColumn(i18n("Modified"));
121 setColumnAlignment(3, Qt::AlignRight
);
122 addColumn(i18n("Permissions"));
123 setColumnAlignment(4, Qt::AlignRight
);
125 addColumn(i18n("First Matching Line"));
126 setColumnAlignment(5, Qt::AlignLeft
);
128 // Disable autoresize for all columns
129 // Resizing is done by resetColumns() function
130 for (int i
= 0; i
< 6; i
++)
131 setColumnWidthMode(i
, Manual
);
135 connect( this, SIGNAL(selectionChanged()),
136 this, SLOT( selectionHasChanged() ));
138 connect(this, SIGNAL(contextMenu(K3ListView
*, Q3ListViewItem
*,const QPoint
&)),
139 this, SLOT(slotContextMenu(K3ListView
*,Q3ListViewItem
*,const QPoint
&)));
141 connect(this, SIGNAL(executed(Q3ListViewItem
*)),
142 this, SLOT(slotExecute(Q3ListViewItem
*)));
143 setDragEnabled(true);
148 QString
KfindWindow::reducedDir(const QString
& fullDir
)
150 if (fullDir
.indexOf(m_baseDir
)==0)
152 QString tmp
=fullDir
.mid(m_baseDir
.length());
158 void KfindWindow::beginSearch(const KUrl
& baseUrl
)
160 kDebug()<<QString("beginSearch in: %1").arg(baseUrl
.path());
161 m_baseDir
=baseUrl
.path(KUrl::AddTrailingSlash
);
162 haveSelection
= false;
166 void KfindWindow::endSearch()
170 void KfindWindow::insertItem(const KFileItem
&item
, const QString
& matchingLine
)
172 new KfFileLVI(this, item
, matchingLine
);
175 // copy to clipboard aka X11 selection
176 void KfindWindow::copySelection()
178 Q3DragObject
*drag_obj
= dragObject();
182 QClipboard
*cb
= kapp
->clipboard();
183 cb
->setData(drag_obj
);
187 void KfindWindow::saveResults()
189 Q3ListViewItem
*item
;
191 KFileDialog
*dlg
= new KFileDialog(QString(), QString(), this);
192 dlg
->setOperationMode (KFileDialog::Saving
);
194 dlg
->setCaption(i18n("Save Results As"));
198 list
<< "text/plain" << "text/html";
200 dlg
->setOperationMode(KFileDialog::Saving
);
202 dlg
->setMimeFilter(list
, QString("text/plain"));
206 KUrl u
= dlg
->selectedUrl();
207 KMimeType::Ptr mimeType
= dlg
->currentFilterMimeType();
210 if (!u
.isValid() || !u
.isLocalFile())
213 QString filename
= u
.path();
215 QFile
file(filename
);
217 if ( !file
.open(QIODevice::WriteOnly
) )
218 KMessageBox::error(parentWidget(),
219 i18n("Unable to save results."));
221 QTextStream
stream( &file
);
222 stream
.setCodec( QTextCodec::codecForLocale() );
224 if ( mimeType
->name() == "text/html") {
225 stream
<< QString::fromLatin1("<HTML><HEAD>\n"
227 "<TITLE>%2</TITLE></HEAD>\n"
230 .arg(i18n("KFind Results File"))
231 .arg(i18n("KFind Results File"))
232 .arg(i18n("KFind Results File"));
237 QString path
=((KfFileLVI
*)item
)->fileitem
.url().url();
238 QString pretty
=Qt::escape(((KfFileLVI
*)item
)->fileitem
.url().prettyUrl());
239 stream
<< QString::fromLatin1("<DT><A HREF=\"") << path
240 << QString::fromLatin1("\">") << pretty
241 << QString::fromLatin1("</A>\n");
243 item
= item
->nextSibling();
245 stream
<< QString::fromLatin1("</DL><P></BODY></HTML>\n");
251 QString path
=((KfFileLVI
*)item
)->fileitem
.url().url();
252 stream
<< path
<< endl
;
253 item
= item
->nextSibling();
258 KMessageBox::information(parentWidget(),
259 i18n("Results were saved to file\n")+
264 // This function is called when selection is changed (both selected/deselected)
265 // It notifies the parent about selection status and enables/disables menubar
266 void KfindWindow::selectionHasChanged()
268 emit
resultSelected(true);
270 Q3ListViewItem
*item
= firstChild();
273 if(isSelected(item
)) {
274 emit
resultSelected( true );
275 haveSelection
= true;
279 item
= item
->nextSibling();
282 haveSelection
= false;
283 emit
resultSelected(false);
286 void KfindWindow::deleteFiles()
288 QString tmp
= i18np("Do you really want to delete the selected file?",
289 "Do you really want to delete the %1 selected files?",selectedItems().count());
290 if (KMessageBox::warningContinueCancel(parentWidget(), tmp
, "", KStandardGuiItem::del()) == KMessageBox::Cancel
)
293 // Iterate on all selected elements
294 QList
<Q3ListViewItem
*> selected
= selectedItems();
295 foreach ( Q3ListViewItem
* listViewItem
, selected
) {
296 KfFileLVI
*item
= static_cast<KfFileLVI
*>(listViewItem
);
297 KFileItem file
= item
->fileitem
;
299 KIO::NetAccess::del(file
.url(), this);
301 qDeleteAll(selected
);
304 void KfindWindow::fileProperties()
306 // This dialog must be modal because it parent dialog is modal as well.
307 // Non-modal property dialog will hide behind the main window
308 KPropertiesDialog::showDialog( ((KfFileLVI
*)currentItem())->fileitem
, this, true );
311 void KfindWindow::openFolder()
313 KFileItem fileitem
= ((KfFileLVI
*)currentItem())->fileitem
;
314 KUrl url
= fileitem
.url();
315 url
.setFileName(QString());
317 (void) new KRun(url
, this);
320 void KfindWindow::openBinding()
322 ((KfFileLVI
*)currentItem())->fileitem
.run();
325 void KfindWindow::slotExecute(Q3ListViewItem
* item
)
329 ((KfFileLVI
*)item
)->fileitem
.run();
332 // Resizes K3ListView to occupy all visible space
333 void KfindWindow::resizeEvent(QResizeEvent
*e
)
335 K3ListView::resizeEvent(e
);
337 clipper()->repaint();
340 Q3DragObject
* KfindWindow::dragObject()
343 QList
<Q3ListViewItem
*> selected
= selectedItems();
345 // create a list of URIs from selection
346 foreach ( Q3ListViewItem
* listViewItem
, selected
)
348 KfFileLVI
*item
= static_cast<KfFileLVI
*>(listViewItem
);
350 uris
.append( item
->fileitem
.url() );
353 if ( uris
.count() <= 0 )
356 Q3UriDrag
*ud
= new K3URLDrag( uris
, (QWidget
*) this );
357 ud
->setObjectName( "kfind uridrag" );
359 const QPixmap
*pix
= currentItem()->pixmap(0);
360 if ( pix
&& !pix
->isNull() )
361 ud
->setPixmap( *pix
);
366 void KfindWindow::resetColumns(bool init
)
368 QFontMetrics fm
= fontMetrics();
371 setColumnWidth(2, qMax(fm
.width(columnText(2)), fm
.width("0000000")) + 15);
373 KGlobal::locale()->formatDateTime(QDateTime::currentDateTime());
374 setColumnWidth(3, qMax(fm
.width(columnText(3)), fm
.width(sampleDate
)) + 15);
375 setColumnWidth(4, qMax(fm
.width(columnText(4)), fm
.width(i18n(perm
[RO
]))) + 15);
376 setColumnWidth(5, qMax(fm
.width(columnText(5)), fm
.width("some text")) + 15);
379 int free_space
= visibleWidth() -
380 columnWidth(2) - columnWidth(3) - columnWidth(4) - columnWidth(5);
382 // int name_w = qMin((int)(free_space*0.5), 150);
383 // int dir_w = free_space - name_w;
384 int name_w
= qMax((int)(free_space
*0.5), fm
.width("some long filename"));
387 setColumnWidth(0, name_w
);
388 setColumnWidth(1, dir_w
);
391 void KfindWindow::slotContextMenu(K3ListView
*,Q3ListViewItem
*item
,const QPoint
&p
)
394 int count
= selectedItems().count();
402 m_menu
= new KMenu(this);
408 //menu = new KMenu(item->text(0), this);
409 m_menu
->addTitle(item
->text(0));
410 m_menu
->addAction(SmallIcon("document-open"),i18nc("Menu item", "Open"), this, SLOT(openBinding()));
411 m_menu
->addAction(SmallIcon("window-new"),i18n("Open Folder"), this, SLOT(openFolder()));
412 m_menu
->addSeparator();
413 m_menu
->addAction(SmallIcon("edit-copy"),i18n("Copy"), this, SLOT(copySelection()));
414 m_menu
->addAction(SmallIcon("edit-delete"),i18n("Delete"), this, SLOT(deleteFiles()));
415 m_menu
->addSeparator();
416 m_menu
->addAction(i18n("Open With..."), this, SLOT(slotOpenWith()));
417 m_menu
->addSeparator();
418 m_menu
->addAction(i18n("Properties"), this, SLOT(fileProperties()));
422 m_menu
->addTitle(i18n("Selected Files"));
423 m_menu
->addAction(SmallIcon("edit-copy"),i18n("Copy"), this, SLOT(copySelection()));
424 m_menu
->addAction(SmallIcon("edit-delete"),i18n("Delete"), this, SLOT(deleteFiles()));
429 void KfindWindow::slotOpenWith()
431 KRun::displayOpenWithDialog( KUrl::split(((KfFileLVI
*)currentItem())->fileitem
.url()), window() );