OSDN Git Service

Conflate name and class columns in GUI package list.
[mingw/mingw-get.git] / src / pkglist.cpp
1 /*
2  * pkglist.cpp
3  *
4  * $Id$
5  *
6  * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
7  * Copyright (C) 2012, 2013, MinGW.org Project
8  *
9  *
10  * Implementation of the methods for the pkgListViewMaker class, and
11  * its AppWindowMaker client API, to support the display of the package
12  * list in the mingw-get graphical user interface.
13  *
14  *
15  * This is free software.  Permission is granted to copy, modify and
16  * redistribute this software, under the provisions of the GNU General
17  * Public License, Version 3, (or, at your option, any later version),
18  * as published by the Free Software Foundation; see the file COPYING
19  * for licensing details.
20  *
21  * Note, in particular, that this software is provided "as is", in the
22  * hope that it may prove useful, but WITHOUT WARRANTY OF ANY KIND; not
23  * even an implied WARRANTY OF MERCHANTABILITY, nor of FITNESS FOR ANY
24  * PARTICULAR PURPOSE.  Under no circumstances will the author, or the
25  * MinGW Project, accept liability for any damages, however caused,
26  * arising from the use of this software.
27  *
28  */
29 #define _WIN32_IE  0x0300
30
31 #include "guimain.h"
32 #include "pkgbase.h"
33 #include "pkglist.h"
34 #include "pkgtask.h"
35 #include "pkgkeys.h"
36 #include "pkginfo.h"
37
38 #include <wtkexcept.h>
39
40 void AppWindowMaker::InitPackageListView()
41 {
42   /* Create and initialise a ListView window, in which to present
43    * the package list...
44    */
45   PackageListView = CreateWindow( WC_LISTVIEW, NULL,
46       WS_VISIBLE | WS_BORDER | WS_CHILD | WS_EX_CLIENTEDGE |
47       LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS, 0, 0, 0, 0,
48       AppWindow, (HMENU)(ID_PACKAGE_LISTVIEW),
49       AppInstance, NULL
50     );
51   /* ...and set its extended style attributes.
52    */
53   ListView_SetExtendedListViewStyle( PackageListView,
54       LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_ONECLICKACTIVATE
55     );
56
57   /* Propagate the default font, as set for the main window itself,
58    * to the list view control.
59    */
60   SendMessage( PackageListView, WM_SETFONT, (WPARAM)(DefaultFont), TRUE );
61
62   /* Assign an image list, to furnish the package status icons.
63    */
64   HIMAGELIST iconlist = ImageList_Create( 16, 16, ILC_COLOR32 | ILC_MASK, 13, 0 );
65   for( int index = 0; index <= PKGSTATE( PURGE ); index++ )
66   {
67     HICON image = LoadIcon( AppInstance, MAKEINTRESOURCE(ID_PKGSTATE(index)) );
68     if( ImageList_AddIcon( iconlist, image ) == -1 )
69       throw WTK::runtime_error( "Error loading package status icons" );
70   }
71   ListView_SetImageList( PackageListView, iconlist, LVSIL_SMALL );
72
73   /* Initialise the table layout, and assign column headings.
74    */
75   LVCOLUMN table;
76   struct { int id; int width; const char *text; } headings[] =
77   {
78     /* Specify the column headings for the package list table.
79      */
80     { ID_PKGLIST_TABLE_HEADINGS,  20, ""  },
81     { ID_PKGNAME_COLUMN_HEADING, 198, "Package" },
82     { ID_INSTVER_COLUMN_HEADING, 125, "Installed Version" },
83     { ID_REPOVER_COLUMN_HEADING, 125, "Repository Version" },
84     { ID_PKGDESC_COLUMN_HEADING, 400, "Description" },
85     /*
86      * This all-null entry terminates the sequence.
87      */
88     { 0, 0, NULL }
89   };
90   table.fmt = LVCFMT_LEFT;
91   table.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
92   for( int index = 0; headings[index].text != NULL; index++ )
93   {
94     /* Loop over the columns, setting the initial width
95      * (in pixels), and assigning the heading to each.
96      */
97     table.cx = headings[index].width;
98     table.pszText = (char *)(headings[index].text);
99     ListView_InsertColumn( PackageListView, index, &table );
100   }
101   /* "Update" the package list, to initialise the list view.
102    */
103   UpdatePackageList();
104 }
105
106 void AppWindowMaker::UpdatePackageList()
107 {
108   /* Scan the XML package catalogue, adding a ListView item entry
109    * for each package record.
110    */
111   pkgListViewMaker PackageList( PackageListView );
112   pkgDirectory *dir = pkgData->CatalogueAllPackages();
113   dir->InOrder( &PackageList );
114   delete dir;
115
116   /* Force a redraw of the application window, to ensure that the
117    * data pane content remains synchronised.
118    */
119   InvalidateRect( AppWindow, NULL, FALSE );
120   UpdateWindow( AppWindow );
121 }
122
123 inline bool pkgXmlNode::IsVisibleGroupMember()
124 {
125   /* Hook invoked before adding a package reference to the package list,
126    * to ensure that it represents a member of the current package group.
127    */
128   return AppWindowMaker::IsPackageGroupAffiliate( this );
129 }
130
131 /* FIXME: Complementary check for component class visibility requires a
132  * future component class visibility control; for the time being, we may
133  * assume that any component package should be visible if it belongs to
134  * any visible package group, (including a component subset of such a
135  * group, which may comprise only itself).
136  */
137 inline bool pkgXmlNode::IsVisibleClass(){ return IsVisibleGroupMember(); }
138
139 static char *pkgGetTitle( pkgXmlNode *pkg, const pkgXmlNode *xml_root )
140 {
141   /* A private helper method, to retrieve the title attribute
142    * associated with the description for the nominated package.
143    *
144    * Note: this should really return a const char *, but then
145    * we would need to cast it to non-const for mapping into the
146    * ill-formed structure of Microsoft's LVITEM, so we may just
147    * as well return the non-const result anyway.
148    */
149   pkgXmlNode *desc = pkg->FindFirstAssociate( description_key );
150   while( desc != NULL )
151   {
152     /* Handling it internally as the const which it should be...
153      */
154     const char *title;
155     if( (title = desc->GetPropVal( title_key, NULL )) != NULL )
156       /*
157        * As soon as we find a description element with an
158        * assigned title attribute, immediately return it,
159        * (with the required cast to non-const).
160        */
161       return (char *)(title);
162
163     /* If we haven't yet found any title attribute, check for any
164      * further description elements at the current XML nesting level.
165      */
166     desc = desc->FindNextAssociate( description_key );
167   }
168
169   /* If we've exhausted all description elements at the current XML
170    * nesting level, without finding a title attribute, and we haven't
171    * checked all enclosing levels back to the document root...
172    */
173   if( pkg != xml_root )
174     /*
175      * ...then continue the search in the immediately enclosing level.
176      */
177     return pkgGetTitle( pkg->GetParent(), xml_root );
178
179   /* If we get to here, then we've searched all levels back to the
180    * document root, and have failed to find any title attribute; we
181    * have nothing to return.
182    */
183   return NULL;
184 }
185
186 static inline char *pkgGetTitle( pkgXmlNode *pkg )
187 {
188   /* Overload the preceding function, to automatically generate
189    * the required reference to the XML document root.
190    */
191   return pkgGetTitle( pkg->GetParent(), pkg->GetDocumentRoot() );
192 }
193
194 static inline
195 char *version_string_copy( char *buf, const char *text, int fill = 0 )
196 {
197   /* Local helper function to construct a package version string
198    * from individual version specific elements of the tarname.
199    */
200   if( text != NULL )
201   {
202     /* First, if a fill character is specified, copy it as the
203      * first character of the result; (we assume that we are
204      * appending to a previously constructed result, and that
205      * this is the field separator character).
206      */
207     if( fill != 0 ) *buf++ = fill;
208
209     /* Now, append "text" up to, and including its final NUL
210      * terminator; (note that we do NOT guard against buffer
211      * overrun, as we have complete control over the calling
212      * context, where we allocated a result buffer at least
213      * as long as the tarname string from which the composed
214      * version string is extracted, and the composed result
215      * can never exceed the original length of this).
216      */
217     do { if( (*buf = *text) != '\0' ) ++buf; } while( *text++ != '\0' );
218   }
219   /* Finally, we return a pointer to the terminating NUL of
220    * the result, so as to facilitate appending further text.
221    */
222   return buf;
223 }
224
225 static char *pkgVersionString( char *buf, pkgSpecs *pkg )
226 {
227   /* Helper method to construct a fully qualified version string
228    * from the decomposed package tarname form in a pkgSpecs structure.
229    *
230    * We begin with the concatenation of package version and build ID
231    * fields, retrieved from the pkgSpecs representation...
232    */
233   const char *pkg_version_tag;
234   char *update = version_string_copy( buf, pkg->GetPackageVersion() );
235   update = version_string_copy( update, pkg->GetPackageBuild(), '-' );
236   if( (pkg_version_tag = pkg->GetSubSystemVersion()) != NULL )
237   {
238     /* ...then, we append the sub-system ID, if applicable.
239      */
240     update = version_string_copy( update, pkg->GetSubSystemName(), '-' );
241     update = version_string_copy( update, pkg_version_tag, '-' );
242     update = version_string_copy( update, pkg->GetSubSystemBuild(), '-' );
243   }
244   if( (pkg_version_tag = pkg->GetReleaseStatus()) != NULL )
245   {
246     /* When the package name also includes a release classification,
247      * then we also append this to the displayed version number (noting
248      * that an initial '$' is special, and should be suppressed)...
249      */
250     if( *pkg_version_tag == '$' ) ++pkg_version_tag;
251     update = version_string_copy( update, pkg_version_tag, '-' );
252     /*
253      * ...followed by any serialisation index for the release...
254      */
255     update = version_string_copy( update, pkg->GetReleaseIndex(), '-' );
256   }
257   /* ...and finally, we return a pointer to the buffer in which
258    * we constructed the fully qualified version string.
259    */
260   return buf;
261 }
262
263 pkgListViewMaker::pkgListViewMaker( HWND pane ): ListView( pane )
264 {
265   /* Constructor: initialise the invariant parameters within the
266    * embedded W32API ListView control structure.
267    */
268   content.stateMask = 0;
269   content.mask = LVIF_PARAM | LVIF_IMAGE | LVIF_STATE;
270   content.iSubItem = 0;
271   content.iItem = -1;
272 }
273
274 EXTERN_C pkgXmlNode *pkgGetStatus( pkgXmlNode *pkg, pkgActionItem *avail )
275 {
276   /* Helper function to acquire release availability and installation
277    * status attributes for a specified package.
278    */
279   pkg = pkg->FindFirstAssociate( release_key );
280   while( pkg != NULL )
281   {
282     /* Examine each available release specification for the nominated
283      * package; select the one which represents the latest (most recent)
284      * available release.
285      */
286     avail->SelectIfMostRecentFit( pkg );
287
288     /* Also check for the presence of an installation record for each
289      * release; if found, mark it as the currently installed release;
290      * (we assign the "to-remove" attribute, but we don't action it).
291      */
292     if( pkg->GetInstallationRecord( pkg->GetPropVal( tarname_key, NULL )) != NULL )
293       avail->SelectPackage( pkg, to_remove );
294
295     /* Cycle, until all known releases have been examined.
296      */
297     pkg = pkg->FindNextAssociate( release_key );
298   }
299   /* Check the identification of any currently installed release; this
300    * will capture property data for any installed release which may have
301    * been subsequently withdrawn from distribution.
302    */
303   avail->ConfirmInstallationStatus();
304
305   /* Finally, return a pointer to the specification for the installed
306    * release, if any, of the package under consideration.
307    */
308   return avail->Selection( to_remove );
309 }
310
311 void pkgListViewMaker::InsertItem( pkgXmlNode *pkg, char *class_name )
312 {
313   /* Private method to add a single package record, as an individual
314    * row item, to the displayed list view table; begin by filling in
315    * the appropriate fields within the "content" structure...
316    */
317   content.iItem++;
318   content.state = 0;
319   content.lParam = (unsigned long)(pkg);
320
321   /* ...then delegate the actual entry construction to...
322    */
323   UpdateItem( class_name, true );
324 }
325
326 inline bool pkgListViewMaker::GetItem( void )
327 {
328   /* An inline helper method to load the content structure from the
329    * next available item, if any, in the package list view; returns
330    * true when content is successfully loaded, else returns false.
331    */
332   content.iItem++; return ListView_GetItem( ListView, &content );
333 }
334
335 void pkgListViewMaker::UpdateItem( char *class_name, bool new_entry )
336 {
337   /* Assign a temporary action item, through which we may identify
338    * the latest available, and currently installed (if any), version
339    * attributes for the package under consideration.
340    */
341   pkgActionItem avail;
342   pkgXmlNode *package = (pkgXmlNode *)(content.lParam);
343   pkgXmlNode *installed = pkgGetStatus( package, &avail );
344
345   /* Decompose the package tarname identifier for the latest available
346    * release, into its individual package specification properties.
347    */
348   pkgSpecs latest( package = avail.Selection() );
349
350   /* Allocate a temporary working text buffer, in which to format
351    * package property values for display...
352    */
353   size_t len = strlen( package->GetPropVal( tarname_key, value_none ) );
354   if( installed != NULL )
355   {
356     /* ...ensuring that it is at least as large as the longer of the
357      * latest or installed release tarname.
358      */
359     size_t altlen = strlen( installed->GetPropVal( tarname_key, value_none ) );
360     if( altlen > len ) len = altlen;
361   }
362   char buf[1 + len];
363
364   /* Choose a suitable icon for representation of the installation
365    * status of the package under consideration...
366    */
367   if( installed != NULL )
368   {
369     /* ...noting that, when it is already installed...
370      */
371     pkgSpecs current( installed );
372     content.iImage = (latest > current)
373       ? /* ...and, when the latest available is NEWER than the
374          * installed version, then we choose the icon indicating
375          * an installed package with an available update...
376          */
377         PKGSTATE( INSTALLED_OLD )
378
379       : /* ...or, when the latest available is NOT NEWER than
380          * the installed version, then we choose the alternative
381          * icon, simply indicating an installed package...
382          */
383         PKGSTATE( INSTALLED_CURRENT );
384
385     /* ...and also, load the version identification string for
386      * the installed version into the working text buffer.
387      */
388     pkgVersionString( buf, &current );
389   }
390   else
391   { /* Alternatively, for any package which is not recorded as
392      * installed, choose the icon indicating an available, but
393      * not (yet) installed package...
394      */
395     content.iImage = PKGSTATE( AVAILABLE );
396
397     /* ...and mark the list view column entry, for the installed
398      * version, as empty.
399      */
400     *buf = '\0';
401   }
402
403   /* When compiling a new list entry...
404    */
405   if( new_entry )
406   {
407     /* ...add the package identification record, as a new item...
408      */
409     ListView_InsertItem( ListView, &content );
410     /*
411      * ...and fill in the text for the package name, class name,
412      * and package description columns...
413      */
414     char name[len]; const char *fmt = "%s-%s";
415     sprintf( name, *class_name ? fmt : fmt + 3, package_name, class_name );
416     ListView_SetItemText( ListView, content.iItem, 1, name );
417     ListView_SetItemText( ListView, content.iItem, 4, pkgGetTitle( package ) );
418   }
419   else
420     /* ...otherwise, this is simply a request to update an
421      * existing item, in place; do so.  (Note that, in this
422      * case, we DO NOT touch the package name, class name,
423      * or package description column content).
424      */
425     ListView_SetItem( ListView, &content );
426
427   /* Always fill in the text, as established above, in the
428    * column which identifies the currently installed version,
429    * if any, or clear it if the package is not installed.
430    */
431   ListView_SetItemText( ListView, content.iItem, 2, buf );
432
433   /* Finally, fill in the text of the column which identifies
434    * the latest available version of the package.
435    */
436   ListView_SetItemText( ListView, content.iItem, 3, pkgVersionString( buf, &latest ) );
437 }
438
439 void pkgListViewMaker::Dispatch( pkgXmlNode *package )
440 {
441   /* Implementation of the "dispatcher" method, which is required
442    * by any derivative of the pkgDirectoryViewerEngine class, for
443    * dispatching the content of the directory to the display service,
444    * (which, in this case, populates the list view window pane).
445    */
446   if( package->IsElementOfType( package_key ) )
447   {
448     /* Assemble the package name into the list view record block.
449      */
450     package_name = (char *)(package->GetPropVal( name_key, value_unknown ));
451
452     /* When processing a pkgDirectory entry for a package entity,
453      * generate a sub-directory listing for any contained package
454      * components...
455      */
456     pkgDirectory *dir;
457     if( (dir = EnumerateComponents( package )) != NULL )
458     {
459       /* ...and recurse, to process any which are found...
460        */
461       dir->InOrder( this );
462       delete dir;
463     }
464     else if( package->IsVisibleGroupMember() )
465       /*
466        * ...otherwise, simply insert an unclassified list entry
467        * for the bare package name, omitting the component class.
468        */
469       InsertItem( package, (char *)("") );
470   }
471   else if( package->IsElementOfType( component_key ) && package->IsVisibleClass() )
472   {
473     /* Handle the recursive calls for the component sub-directory,
474      * inheriting the package name entry from the original package
475      * entity, and emit an appropriately classified list view entry
476      * for each identified component package.
477      */
478     InsertItem( package, (char *)(package->GetPropVal( class_key, "" )) );
479   }
480 }
481
482 void pkgListViewMaker::MarkScheduledActions( pkgActionItem *schedule )
483 {
484   /* Method to reassign icons to entries within the package list view,
485    * indicating any which have been marked for installation, upgrade,
486    * or removal of the associated package.
487    */
488   if( schedule != NULL )
489     for( content.iItem = -1; GetItem(); )
490     {
491       /* Visiting every entry in the list...
492        */
493       pkgActionItem *ref;
494       if( (ref = schedule->GetReference( (pkgXmlNode *)(content.lParam) )) != NULL )
495       {
496         /* ...identify those which are associated with a scheduled action...
497          */
498         unsigned long opcode;
499         if( (opcode = ref->HasAttribute( ACTION_MASK )) == ACTION_INSTALL )
500         {
501           /* ...selecting the appropriate icon to mark those packages
502            * which have been scheduled for installation...
503            *
504            * FIXME: we should also consider that such packages
505            * may have been scheduled for reinstallation.
506            */
507           content.iImage = PKGSTATE( AVAILABLE_INSTALL );
508         }
509         else if( opcode == ACTION_UPGRADE )
510         {
511           /* ...those which have been scheduled for upgrade...
512            */
513           content.iImage = PKGSTATE( UPGRADE );
514         }
515         else if( opcode == ACTION_REMOVE )
516         {
517           /* ...and those which have been scheduled for removal.
518            */
519           content.iImage = PKGSTATE( REMOVE );
520         }
521         else
522         { /* Where a scheduled action is any other than those above,
523            * handle as if there was no scheduled action...
524            */
525           opcode = 0UL;
526           /*
527            * ...and ensure that the list view entry reflects the
528            * normal display state for the associated package.
529            */
530           UpdateItem( NULL );
531         }
532         if( opcode != 0UL )
533           /*
534            * Where an action mark is appropriate, ensure that it
535            * is applied to the list view entry.
536            */
537           ListView_SetItem( ListView, &content );
538       }
539     }
540 }
541
542 void pkgListViewMaker::UpdateListView( void )
543 {
544   /* A simple helper method, to refresh the content of the
545    * package list view, resetting each item to its initial
546    * unmarked display state.
547    */
548   for( content.iItem = -1; GetItem(); ) UpdateItem( NULL );
549 }
550
551 /* $RCSfile$: end of file */