OSDN Git Service

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